Flutter Facebook登录网络 [英] Flutter Facebook Login in web

查看:169
本文介绍了Flutter Facebook登录网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过 Flutter Facebook登录程序包,它在android中可以正常工作,但在网络中我不是被重定向到Facebook进行身份验证.尝试过此软件包的人可以提供帮助吗?

I have tried the Flutter Facebook Login package, it works properly in android but in web I am not being redirected to the Facebook for authentication. Can someone who tried this package help?

推荐答案

此插件不支持网络.
但是有人更新了代码以支持它 romulojunjunor flutter_facebook_login
如果您想使用它:

This plugin does not support web.
But someone has updated the code to support it romulojjunior flutter_facebook_login
If you wish to use it:

  flutter_facebook_login:
    git:
      url: git@github.com:romulojjunior/flutter_facebook_login.git
      ref: v1.3.0-web

还可以尝试 firebase_auth Facebook身份验证:

Could also try firebase_auth for Facebook auth:

import 'dart:html' as html;

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

final FirebaseAuth _auth = FirebaseAuth.instance;

void main() {
  String token;
  if (html.window.location.href.contains("access_token")) {
    String url = html.window.location.href.replaceFirst("#/", "?"); // workaround for readable redirect url
    Uri uri = Uri.parse(url);
    if (uri.queryParameters.keys.contains("access_token")) token = uri.queryParameters["access_token"];
  }

  runApp(
    MaterialApp(
        title: 'Facebook Sign In',
        home: SignIn(
          token: token,
        )),
  );
}

class SignIn extends StatefulWidget {
  final String token;

  const SignIn({Key key, this.token}) : super(key: key);

  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  String _message;
  final String clientId = "FBClientId";
  final String redirectUri = "http://localhost";

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (widget.token != null) _signInWithFacebook(widget.token);
  }

  void _signInWithFacebook(String token) async {
    setState(() {
      _message = "Loading...";
    });
    final AuthCredential credential = FacebookAuthProvider.getCredential(
      accessToken: token,
    );
    final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
    assert(await user.getIdToken() != null);
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    setState(() {
      if (user != null) {
        _message = 'Successfully signed in with Facebook. ' + user?.displayName.toString();
      } else {
        _message = 'Failed to sign in with Facebook. ';
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: <Widget>[
            Text(_message ?? "Please try to sign in"),
            RaisedButton(
              onPressed: () {
                html.window.open(
                    "https://www.facebook.com/dialog/oauth?response_type=token&scope=email,public_profile,&client_id=${clientId}&redirect_uri=${redirectUri}",
                    "_self");
              },
              child: Text('Facebook login'),
            ),
          ],
        ),
      ),
    );
  }
}

别忘了为Web添加Firebase: README.md
为Firebase启用Facebook登录(1-3,然后开始步骤): firebase-facebook

Don't forget to add firebase for web: README.md
Enable facebook login for firebase (1-3 Before you begin steps): firebase-facebook

这篇关于Flutter Facebook登录网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆