【问题标题】:Update a Firebase anonymous user using Email & Password使用电子邮件和密码更新 Firebase 匿名用户
【发布时间】:2020-04-27 20:48:41
【问题描述】:

人们可以匿名登录我的应用:

FirebaseAuth.instance.signInAnonymously();

进入后,他们有一个小指示器提醒他们是否希望能够跨手机保存数据,他们需要登录。对于看起来像这样的 Google 身份验证:

Future<void> anonymousGoogleLink() async {
  try {
    final user = await auth.currentUser();
    final credential = await googleCredential();

    await user.linkWithCredential(credential);
  } catch (error) {
    throw _errorToHumanReadable(error.toString());
  }
}

googleCredential 在哪里:


Future<AuthCredential> googleCredential() async {
  final googleUser = await _googleSignIn.signIn();

  if (googleUser == null) throw 'User Canceled Permissions';

  final googleAuth = await googleUser.authentication;
  final signInMethods = await auth.fetchSignInMethodsForEmail(
    email: googleUser.email,
  );

  if (signInMethods.isNotEmpty && !signInMethods.contains('google.com')) {
    throw 'An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.';
  }

  return GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
}

现在我想做同样的事情,但将当前匿名帐户与电子邮件和密码身份验证相关联。

编辑:

在写这篇文章时找到了一个可能的解决方案。留给别人可能看到。请随时纠正我。

【问题讨论】:

    标签: firebase flutter firebase-authentication


    【解决方案1】:

    FirebaseUser 对象上,方法updatePassword 有这个文档字符串:

    /// Updates the password of the user.
    ///
    /// Anonymous users who update both their email and password will no
    /// longer be anonymous. They will be able to log in with these credentials.
    /// ...
    

    您应该能够同时更新两者,然后他们将作为普通用户进行身份验证。对我来说,看起来像:

    Future<void> anonymousEmailLink({
      @required FirebaseUser user,
      @required String email,
      @required String password,
    }) async {
      try {
        await Future.wait([
          user.updateEmail(email),
          user.updatePassword(password),
        ]);
      } catch (error) {
        throw _errorToHumanReadable(error.toString());
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-27
      • 2012-07-23
      • 1970-01-01
      • 2017-08-26
      • 2017-12-12
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多