[英]Using Firebase reauthenticate
I'll appreciate assistance with how to reauthenticate a user in Firebase.我将感谢有关如何在 Firebase 中重新验证用户身份的帮助。 I wonder if it makes any sense adding all these great features if the documentation doesn't explain how to use it:如果文档没有解释如何使用它,我想知道添加所有这些强大的功能是否有意义:
Currently, this is what I'm trying, and it ain't working.目前,这就是我正在尝试的,但它不起作用。 Errors as cannot read property 'credential' of undefined
cannot read property 'credential' of undefined
错误
In constructor:在构造函数中:
constructor(@Inject(FirebaseApp) firebaseApp: any) {
this.auth = firebaseApp.auth();
console.log(this.auth);
}
then the function那么函数
changePassword(passwordData) {
if(passwordData.valid) {
console.log(passwordData.value);
// let us reauthenticate first irrespective of how long
// user's been logged in!
const user = this.auth.currentUser;
const credential = this.auth.EmailAuthProvider.credential(user.email, passwordData.value.oldpassword);
console.log(credential);
this.auth.reauthenticate(credential)
.then((_) => {
console.log('User reauthenticated');
this.auth.updatePassword(passwordData.value.newpassword)
.then((_) => {
console.log('Password changed');
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error);
})
}
}
The reauthenticate()
method is called on a firebase.User , not on firebase.auth.Auth itself. reauthenticate()
方法在firebase.User上调用,而不是在firebase.auth.Auth本身上调用。
var user = firebase.app.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential('puf@firebaseui.com', 'firebase');
user.reauthenticate(credentials);
Update (July 2017) :更新(2017 年 7 月) :
There are some breaking change in the 4.0 version of the Firebase Web SDK. Firebase Web SDK 4.0 版中有一些重大变化。 From the release notes :从发行说明:
BREAKING:
firebase.User.prototype.reauthenticate
has been removed in favor offirebase.User.prototype.reauthenticateWithCredential
.突破:firebase.User.prototype.reauthenticate
已被删除,取而代之的是firebase.User.prototype.reauthenticateWithCredential
。
As far as I can tell the reauthenticateWithCredential
is a drop-in replacement for the old method.据我所知, reauthenticateWithCredential
是旧方法的替代品。
Here's some code that enabled users to (a) reauthenticate in Firebase and (b) change their passwords after reauthenticating for me.下面是一些代码,使用户能够 (a) 在 Firebase 中重新进行身份验证和 (b) 在为我重新进行身份验证后更改他们的密码。 I researched for about an hour while writing this, so hopefully it saves someone a minute.我在写这篇文章的时候研究了大约一个小时,所以希望它能为某人节省一分钟。
Wrote in VueJS:用 VueJS 写的:
changePassword() {
let self = this; // i use "self" to get around scope issues
var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(
this.$store.state.userId, // references the user's email address
this.oldPassword
);
user.reauthenticateWithCredential(credential)
.then(function() {
// User re-authenticated.
user.updatePassword(self.newPassword)
.then(function() {
console.log("Password update successful!");
})
.catch(function(error) {
console.log(
"An error occurred while changing the password:",
error
);
});
})
.catch(function(error) {
console.log("Some kinda bug: ", error);
// An error happened.
});
Slight changes as of May 2019, see more details here .截至 2019 年 5 月略有变化,请在此处查看更多详细信息。 Code is as follows:代码如下:
var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(user.email, password);
// Prompt the user to re-provide their sign-in credentials
return user.reauthenticateWithCredential(credential);
Call changeEmail("new email","password")
in onPressed
directly to update the user email with no reauthentication required error直接在onPressed
调用changeEmail("new email","password")
以更新用户电子邮件,无需重新onPressed
验证错误
RaisedButton(
onPressed: () {
changeEmail(_emailController.text, _passwordController.text);
}
Future<void> changeEmail(String email, String password) async {
User user = await FirebaseAuth.instance.currentUser;
print(email);
print(password);
try {
try {
var authResult = await user.reauthenticateWithCredential(
EmailAuthProvider.getCredential(
email: user.email,
password: password,
),
);
user.updateEmail(email).then((_) {
print("Succesfull changed email");
_backthrow();
}).catchError((error) {
showAlertDialog(context, error.message);
print("email can't be changed" + error.toString());
});
return null;
} catch (e) {
print("2");
}
} catch (e) {
print(e.message);
showAlertDialog(context, e.message);
}
}
Hers a full example how to reauthenticate with Firebase她是如何使用 Firebase 重新进行身份验证的完整示例
var pass = "abcdefg";
var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(user.email, pass);
user.reauthenticateWithCredential(credential).then(() => {
console.log("Its good!");
}).catch((error) => {
console.log(error);
});
I was getting that re-authentication error auth/requires-recent-login
when saving the primary email.保存主电子邮件时,我收到重新验证错误auth/requires-recent-login
。
I couldn't figure out how to implement that poorly documented reauthenticateWithCredential(credential)
method, so, I simply logged-out the user and redirected to login page.我无法弄清楚如何实现那个记录不完整的reauthenticateWithCredential(credential)
方法,因此,我只是注销了用户并重定向到登录页面。 It's a hack but It works like charm!这是一个黑客,但它的作用就像魅力!
firebase.auth().signOut();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.