I'm making a password change page. The user will need to enter their current password to go to the password change section. But I don't know how to get current password. How can I do that?
CodePudding user response:
If you want to reset the password you can try this Reset Password | Firebase Authentication
Not sure you can access user's password because when you look at users in Firebase you can't see password champ.
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
CodePudding user response:
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. 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.
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. If you're asking what if user logged in with emailA
and enter EmailB
to re-authenticate? 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.
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.
Now you're ready to do any sensitive actions, you can delete user account or let them request for changing their password.