使用 Firebase 重新验证
Using Firebase reauthenticate
对于如何在 Firebase 中重新验证用户的帮助,我将不胜感激。我想知道如果文档没有解释如何使用它,添加所有这些强大的功能是否有意义:
目前,这就是我正在尝试的方法,但它不起作用。错误为 cannot read property 'credential' of undefined
在构造函数中:
constructor(@Inject(FirebaseApp) firebaseApp: any) {
this.auth = firebaseApp.auth();
console.log(this.auth);
}
然后函数
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);
})
}
}
reauthenticate()
方法在 firebase.User, not on firebase.auth.Auth 本身上调用。
var user = firebase.app.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential('puf@firebaseui.com', 'firebase');
user.reauthenticate(credentials);
更新(2017 年 7 月):
4.0 版的 Firebase Web SDK 中有一些重大更改。来自 release notes:
BREAKING: firebase.User.prototype.reauthenticate
has been removed in favor of firebase.User.prototype.reauthenticateWithCredential
.
据我所知,reauthenticateWithCredential
是旧方法的直接替代方法。
这里有一些代码可以让用户 (a) 在 Firebase 中重新进行身份验证,以及 (b) 在为我重新进行身份验证后更改他们的密码。我在写这篇文章的时候研究了大约一个小时,所以希望它能为别人节省一分钟。
在 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.
});
自 2019 年 5 月起略有变化,查看更多详细信息here。代码如下:
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);
我在保存主电子邮件时收到重新验证错误 auth/requires-recent-login
。
我无法弄清楚如何实现记录不完整的 reauthenticateWithCredential(credential)
方法,因此,我只是注销了用户并重定向到登录页面。这是一个 hack,但它很有魅力!
firebase.auth().signOut();
直接在onPressed
中调用changeEmail("new email","password")
更新用户邮箱,无需重新验证错误
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);
}
}
她提供了如何使用 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);
});
对于如何在 Firebase 中重新验证用户的帮助,我将不胜感激。我想知道如果文档没有解释如何使用它,添加所有这些强大的功能是否有意义:
目前,这就是我正在尝试的方法,但它不起作用。错误为 cannot read property 'credential' of undefined
在构造函数中:
constructor(@Inject(FirebaseApp) firebaseApp: any) {
this.auth = firebaseApp.auth();
console.log(this.auth);
}
然后函数
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);
})
}
}
reauthenticate()
方法在 firebase.User, not on firebase.auth.Auth 本身上调用。
var user = firebase.app.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential('puf@firebaseui.com', 'firebase');
user.reauthenticate(credentials);
更新(2017 年 7 月):
4.0 版的 Firebase Web SDK 中有一些重大更改。来自 release notes:
BREAKING:
firebase.User.prototype.reauthenticate
has been removed in favor offirebase.User.prototype.reauthenticateWithCredential
.
据我所知,reauthenticateWithCredential
是旧方法的直接替代方法。
这里有一些代码可以让用户 (a) 在 Firebase 中重新进行身份验证,以及 (b) 在为我重新进行身份验证后更改他们的密码。我在写这篇文章的时候研究了大约一个小时,所以希望它能为别人节省一分钟。
在 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.
});
自 2019 年 5 月起略有变化,查看更多详细信息here。代码如下:
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);
我在保存主电子邮件时收到重新验证错误 auth/requires-recent-login
。
我无法弄清楚如何实现记录不完整的 reauthenticateWithCredential(credential)
方法,因此,我只是注销了用户并重定向到登录页面。这是一个 hack,但它很有魅力!
firebase.auth().signOut();
直接在onPressed
中调用changeEmail("new email","password")
更新用户邮箱,无需重新验证错误
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);
}
}
她提供了如何使用 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);
});