如何在flutter中链接firebase_auth 0.11.1中的firebase帐户? [英] How do you link firebase accounts in firebase_auth 0.11.1 in flutter?

查看:61
本文介绍了如何在flutter中链接firebase_auth 0.11.1中的firebase帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用firebase_auth 0.11.0成功地实现了Flutter的Google和Facebook登录.我需要为具有相同电子邮件地址的帐户实现链接帐户功能.

I've successfully implemented Google and Facebook sign ins for Flutter using firebase_auth 0.11.0. I need to implement the linking account feature for accounts that have the same email address.

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()上捕获错误,但无济于事.

How/where do you catch the Firebase error (auth/account-exists-with-different-credential) that the account is a duplicate email and therefore requires the linkwithcredential function? I've tried to catch the error on signInWithCredential and _signInWithGoogle() above to no avail.

基于此处的示例:

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);

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

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

This is how you link the accounts, but my question is how do you determine that this function needs to be run?

为清楚起见,当只有一个凭据/会话时,我可以链接帐户吗?例如,该流应为,使用Facebook登录,捕获(ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL),然后链接到Google.

for clarity, can I link the accounts when only one credential/session exists? For example, the flow should be, sign in with Facebook, catch (ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL), and then link to Google.

推荐答案

要链接google和facebook,我正在使用:

To link google and facebook, I'm using:

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);
            }
          }
        }
      });

这篇关于如何在flutter中链接firebase_auth 0.11.1中的firebase帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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