简体   繁体   English

Flutter Firebase Auth 如何获取用户当前密码?

[英]Flutter Firebase Auth How To Get User Current Password?

I'm making a password change page.我正在制作密码更改页面。 The user will need to enter their current password to go to the password change section.用户需要在密码更改部分输入他们当前的密码 go。 But I don't know how to get current password.但我不知道如何获取当前密码。 How can I do that?我怎样才能做到这一点?

If you want to reset the password you can try this Reset Password |如果你想重设密码,你可以试试这个重设密码 | Firebase Authentication Firebase 认证

Not sure you can access user's password because when you look at users in Firebase you can't see password champ.不确定您是否可以访问用户的密码,因为当您查看 Firebase 中的用户时,您看不到密码冠军。
Doing this you'll have all user's data available:这样做您将获得所有用户的数据:

User? user = FirebaseAuth.instance.currentUser;
String userName = user!.providerData.first.displayName.toString(); // Display user's name
String userPhoto = user!.providerData.first.photoURL.toString(); // Display user's photo
                                             

user.providerData.first you'll have access user's informations but the password seems to not be available user.providerData.first您将可以访问用户的信息,但密码似乎不可用

This action is one of the sensitive actions that requires to re-authenticate the user before proceeding.此操作是需要在继续之前重新验证用户身份的敏感操作之一。 You can read more about it here .你可以在这里阅读更多关于它的信息。

Now you should ask user to re-authenticate again, if you used any provider you can get the credential from Firebase and pass to reauthenticateWithCredential function.现在您应该要求用户再次重新验证,如果您使用任何提供程序,您可以从 Firebase 获取凭证并传递给reauthenticateWithCredential function。 But in your case that used password provider, meaning using email and password to sign up users, you should show a screen to users and ask them to enter their email and password again and then use that information and re-authenticate them.但是在您使用password提供程序的情况下,这意味着使用emailpassword来注册用户,您应该向用户显示一个屏幕并要求他们再次输入emailpassword ,然后使用该信息并重新验证他们。

Simple example:简单的例子:

Let's say we showed a screen to user and got their email and password from them and then we can use this information to re-authenticate user.假设我们向用户显示了一个屏幕并从他们那里获得了他们的 email 和密码,然后我们可以使用此信息重新验证用户。 If you're asking what if user logged in with emailA and enter EmailB to re-authenticate?如果您问如果用户使用emailA登录并输入EmailB以重新认证怎么办? The answer is that Firebase will throw an Exception( user-mismatch ), so you can ask user to enter the email that they are currently logged in with.答案是 Firebase 会抛出异常( user-mismatch ),所以你可以要求用户输入他们当前登录的 email。

Future<void> reauthenticateWithCredential(String email, String password) async {
  try {
    final user = FirebaseAuth.instance.currentUser;
    final credential = EmailAuthProvider.credential(
      email: email,
      password: password,
    );
    await user.reauthenticateWithCredential(credential);
  } on Exception catch (e) {
    // Handle exceptions
  }
}

And to get credentials from other providers like google , is similar to log in process, you use the same code, and then get the credential and pass it to reauthenticateWithCredential function to re-authenticate user again.并且从google等其他提供商获取凭据类似于登录过程,您使用相同的代码,然后获取凭据并将其传递给reauthenticateWithCredential function 以再次重新验证用户。

Now you're ready to do any sensitive actions, you can delete user account or let them request for changing their password.现在您已准备好执行任何敏感操作,您可以删除用户帐户或让他们请求更改密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM