Flutter:如何获取Firebase身份验证凭证以更新电子邮件和密码

时间:2019-11-01 02:53:36

标签: android firebase flutter firebase-authentication

所以,在我的Flutter应用中,我试图添加功能来更改电子邮件。

我使用了userData.updateEmail(email)方法,但出现此错误:

未处理的异常:PlatformException(ERROR_REQUIRES_RECENT_LOGIN,此操作敏感,需要最近的身份验证。重试此请求之前,请再次登录。,空)

在网上了解一种解决方案时,我需要使用以下方法重新验证用户身份: userData.reauthenticateWithCredential(credential)

但是我找不到找到将凭据传递给reauthenticateWithCredential方法的方法。

一些代码片段(我认为它们是不必要的):

initUserData() async {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();

    setState(() {
      userData = user;
    });
  }

updateEmail(String value) async {
    // value is the email user inputs in a textfield and is validated
    userData.updateEmail(value);

  }

注意:我同时使用Google登录名和密码电子邮件登录名。

4 个答案:

答案 0 :(得分:2)

有一个重新认证方法。您只需要获取用户的密码即可调用该方法。

FirebaseUser user = await FirebaseAuth.instance.currentUser();
AuthResult authResult = await user.reauthenticateWithCredential(
  EmailAuthProvider.getCredential(
    email: user.email,
    password: password,
  ),
);

// Then use the newly re-authenticated user
authResult.user

答案 1 :(得分:1)

我也遇到了这个问题,我找到了解决方案。您可以像这样使用的提供商获取AuthCredential:

EmailAuthProvider.getCredential(email: 'email', password: 'password');

GoogleAuthProvider.getCredential(idToken: '', accessToken: '')

这两个方法都返回您要查找的内容。

答案 2 :(得分:0)

如果要在Firebase上更改敏感信息,则需要先使用当前凭据对帐户重新进行身份验证,然后才能对其进行更新。 当前flutter没有针对Firebase的reAuthenticate方法,因此您需要调用 signInWithEmailAndPassword 或任何其他signIn方法。

答案 3 :(得分:0)

对我有用

Future resetEmail(String newEmail) async {
    var message;
    FirebaseUser firebaseUser = await _firebaseAuth.currentUser();
    firebaseUser
        .updateEmail(newEmail)
        .then(
          (value) => message = 'Success',
        )
        .catchError((onError) => message = 'error');
    return message;
  } 
相关问题