无法在Flutter App中将Firebase匿名用户转换为永久用户

mrfwxfqh  于 7个月前  发布在  Flutter
关注(0)|答案(2)|浏览(40)

我试图转换一个firebase用户从匿名到永久使用电子邮件/密码和运行到错误。
文档和其他文章指出,我应该linkWithProvider,但它抛出了一个异常,电子邮件应首先验证,然而,这样做抛出另一个异常,要求我先登录。
以下是我迄今为止尝试的一些片段

final emailAuthCredential = EmailAuthProvider.credential(
  email: email,
  password: password,
);

await currentUser.linkWithProvider(emailAuthCredential);

字符串
FirebaseAuthException([firebase_auth/operation-not-allowed]请在更改电子邮件之前验证新电子邮件。

final emailAuthCredential = EmailAuthProvider.credential(
  email: email,
  password: password,
);

await currentUser.verifyBeforeUpdateEmail(email);


FirebaseAuthException([firebase_auth/requires-recent-login]此操作是敏感的,需要最近的身份验证。请在重试此请求之前重新登录。)

zpf6vheq

zpf6vheq1#

您可以使用此代码

linkUser()async{
User? user = FirebaseAuth.instance.currentUser;

if (user != null && user.isAnonymous) {
  try {
    // Prompt the user to enter their email and password
    String email = '[email protected]'; // Replace with the user's input
    String password = 'password123';   // Replace with the user's input

    // Link the user to the email and password provider
    AuthCredential credential = EmailAuthProvider.credential(
      email: email,
      password: password,
    );
    await user.linkWithCredential(credential);

    // Now, the user is converted to a permanent user with email and password
  } catch (e) {
    print('Error linking anonymous user: $e');
  }
}

字符串
}

sigwle7e

sigwle7e2#

解决方案在文档中(duh.)。你现在必须使用身份平台API并使用匿名令牌调用注册。

相关问题