首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在flutter中链接firebase_auth 0.11.1中的firebase帐户?

如何在flutter中链接firebase_auth 0.11.1中的firebase帐户?
EN

Stack Overflow用户
提问于 2019-05-22 23:26:06
回答 2查看 3.6K关注 0票数 3

我已经成功地使用firebase_auth 0.11.0为Flutter实现了谷歌和脸书的登录。我需要为具有相同电子邮件地址的帐户实施链接帐户功能。

代码语言:javascript
复制
Future<FirebaseUser> _signInWithGoogle() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
  accessToken: googleAuth.accessToken,
  idToken: googleAuth.idToken,
);

final FirebaseUser user = await _auth.signInWithCredential(credential);

如何/在哪里捕获Firebase错误(auth/account-exists-with-different-credential),即该帐户是重复的电子邮件,因此需要linkwithcredential函数?我尝试在上面的signInWithCredential_signInWithGoogle()上捕获错误,但都无济于事。

基于这里的示例:

https://github.com/flutter/plugins/blob/06256967e494e6d719023a249c8bdaae4b3ae065/packages/firebase_auth/test/firebase_auth_test.dart

FirebaseUser user = await auth.currentUser(); user = await user.linkWithCredential(credential);

这是您链接帐户的方式,但我的问题是,您如何确定需要运行此函数?

编辑:为了清楚起见,当只有一个凭据/会话存在时,我是否可以链接帐户?例如,流程应该是,登录Facebook,捕获(ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL),,然后链接到谷歌。

EN

回答 2

Stack Overflow用户

发布于 2020-04-04 01:03:03

为了将google和facebook联系起来,我使用:

代码语言:javascript
复制
FacebookLoginResult facebookLoginResult = await _handleFBSignIn();
    final accessToken = facebookLoginResult.accessToken.token;
    if (facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
      final facebookAuthCred =
      FacebookAuthProvider.getCredential(accessToken: accessToken);

      final user = await auth.signInWithCredential(facebookAuthCred).catchError((e) async {
        if (e.code == 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL') {
          var graphResponse = await http.get(
              'https://graph.facebook.com/v2.12/me?fields=name,first_name,last_name,email,picture.width(400)&access_token=${facebookLoginResult
                  .accessToken.token}');
          Map<String, dynamic> profile = json.decode(graphResponse.body);
          profileData = profile;

          var email = profile["email"];

          final signInMethods = await FirebaseAuth.instance
              .fetchSignInMethodsForEmail(email: email);
          if (signInMethods.contains("google.com")) {
            final authGoogleResult = await googleLogin();

            if (authGoogleResult.user.email == e.email) {
              await authGoogleResult.user.linkWithCredential(e.credential);
            }
          }
        }
      });
票数 2
EN

Stack Overflow用户

发布于 2019-05-23 04:39:41

基于这些信息:Linking multiple auth providers with Firebase on Login

(如果它仍然是准确的)我的流量是无法达到的,即你不能在没有登录到第一个firebase帐户的情况下链接帐户,例如登录到Google,然后登录并链接Facebook。

我需要的用户流将需要通过启用同一电子邮件的多个帐户来实现。虽然这会带来其他问题,但第二个firebase帐户用户FirebaseUser user不包含电子邮件,user.emailnull。虽然user.providerData有这封电子邮件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56260249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档