【问题标题】:Flutter: How to get Firebase Auth Credentials to update email and passwordFlutter:如何获取 Firebase 身份验证凭据以更新电子邮件和密码
【发布时间】:2020-02-27 09:58:38
【问题描述】:

所以,在我的颤振应用程序中,我正在尝试添加更改电子邮件的功能。

我使用了 userData.updateEmail(email) 方法,但它给出了这个错误:

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

在网上冲浪时我了解到,我需要通过这种方法重新验证用户身份: userData.reauthenticateWithCredential(凭据)

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

一些代码sn-ps(我觉得它们是不必要的):

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

  }

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

【问题讨论】:

    标签: android firebase flutter firebase-authentication


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 这是否意味着我必须将用户的电子邮件和密码存储在内存中,然后如果我想重新验证,则将它们传递给 getCredential?似乎做错了事
    • 嗯,我认为你应该保存电子邮件,并要求用户在更新电子邮件时输入密码。
    • 我如何对 Firebase 电话身份验证进行重新身份验证?
    【解决方案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
    

    【讨论】:

      【解决方案3】:

      它对我有用

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 嗯,Flutter 有 reauthenticateWithCredential 方法,但它需要密码。如何在不让用户再次输入密码的情况下获得密码?
        • 除非您在第一次连接时使用 sharedPreferences 或数据库(我不太推荐)保存用户密码。我不认为你真的有其他选择。当您想要更改敏感信息时,这是经典的安全性。就像您想更改新密码时输入旧密码一样。
        猜你喜欢
        • 2019-03-15
        • 2021-11-21
        • 2020-10-19
        • 2016-11-13
        • 1970-01-01
        • 2023-01-31
        • 2020-11-22
        • 2019-07-13
        • 1970-01-01
        相关资源
        最近更新 更多