我要做什么?
(this.state.currentPassword)
我要面对什么问题?
var user = firebaseApp.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(
firebaseApp.auth().currentUser.email,
this.state.currentPassword
);
// Prompt the user to re-provide their sign-in credentials
// STEP 1 : re-authenticate user
user.reauthenticateAndRetrieveDataWithCredential(credential).then(function() {
// User re-authenticated.
//Upon re-authenticating, update password
//STEP 2 : UPDATE PASSWORD
var newPassword = firebase.getASecureRandomPassword();
user.updatePassword(newPassword).then(function() {
alert("SUCCESS")
// Update successful.
}).catch(function(error) {
console.log(error)
// An error happened.
});
}).catch(function(error) {
// An error happened.
alert("Error Reauthenticating")
});
答案 0 :(得分:0)
不确定是什么问题,但可以通过以下代码获得所需的输出。
贷记到this article。
reauthenticate = (currentPassword) => {
var user = firebase.auth().currentUser;
var cred = firebase.auth.EmailAuthProvider.credential(
user.email, currentPassword);
return user.reauthenticateAndRetrieveDataWithCredential(cred);
}
changePassword = (currentPassword, newPassword) => {
this.reauthenticate(currentPassword).then(() => {
var user = firebase.auth().currentUser;
user.updatePassword(newPassword).then(() => {
console.log("Password updated!");
}).catch((error) => {
console.log(error);
});
}).catch((error) => {
console.log(error);
});
}
可能需要ES6语法,但明天会查询。