Flutter Facebook Login in web

╄→гoц情女王★ 提交于 2020-06-27 16:42:06

问题


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?


回答1:


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

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'),
            ),
          ],
        ),
      ),
    );
  }
}

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



来源:https://stackoverflow.com/questions/62471919/flutter-facebook-login-in-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!