我正在为正在使用的应用程序上的帐户构建一个设置组件,并且希望用户能够从这些帐户设置中更新其密码。我创建了这个firebase函数:
updateUserPassword(password) {
this.currentUser.updatePassword(password).then(function() {
console.log('succcess!')
}).catch(function(error) {
alert(error)
});
}
我知道我需要重新进行身份验证才能执行这样的请求,但是我似乎无法弄清楚该怎么做!有什么建议吗?
答案 0 :(得分:2)
为了重新验证用户的身份,基本上,您会给电子邮件和密码作为参数。在下面,我将向您显示我的account.ts页面,该页面上有一个按钮供用户更改密码。当用户单击按钮时,将出现带有输入的警报提示:
account.ts
changePassword(){
console.log('Change Password Button Clicked');
//Creating the promt alert with inputs
let alert = this.alertCtrl.create({
title: 'Change Password',
inputs: [
{
name: 'oldPassword',
placeholder: 'Your old password..',
type: 'password'
},
{
name: 'newPassword',
placeholder: 'Your new password..',
type: 'password'
},
{
name: 'newPasswordConfirm',
placeholder: 'Confirm your new password..',
type: 'password'
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: data => {
console.log('Cancel clicked');
}
},
{
text: 'Update Password',
handler: data => {
//First you get the current logged in user
const cpUser = firebase.auth().currentUser;
/*Then you set credentials to be the current logged in user's email
and the password the user typed in the input named "old password"
where he is basically confirming his password just like facebook for example.*/
const credentials = firebase.auth.EmailAuthProvider.credential(
cpUser.email, data.oldPassword);
//Reauthenticating here with the data above
cpUser.reauthenticateWithCredential(credentials).then(
success => {
if(data.newPassword != data.newPasswordConfirm){
let alert = this.alertCtrl.create({
title: 'Change Password Failed',
message: 'You did not confirm your password correctly.',
buttons: ['Try Again']
});
alert.present();
} else if(data.newPassword.length < 6){
let alert = this.alertCtrl.create({
title: 'Change Password Failed',
message: 'Your password should be at least 6 characters long',
buttons: ['Try Again']
});
alert.present();
} else {
let alert = this.alertCtrl.create({
title: 'Change Password Success',
message: 'Your password has been updated!',
buttons: ['OK']
});
alert.present();
/* Update the password to the password the user typed into the
new password input field */
cpUser.updatePassword(data.newPassword).then(function(){
//Success
}).catch(function(error){
//Failed
});
}
},
error => {
console.log(error);
if(error.code === "auth/wrong-password"){
let alert = this.alertCtrl.create({
title: 'Change Password Failed',
message: 'Your old password is invalid.',
buttons: ['Try Again']
});
alert.present();
}
}
)
console.log(credentials);
}
}
]
});
alert.present();
}
答案 1 :(得分:0)
如果用户的登录时间过长,并且您想更改重要信息(例如密码),则用户需要重新认证才能进行此过程。官方的Firebase文档中有大量与此相关的信息。