如何创建 Firebase web user.reauthenticateWithCredential() 方法所需的 "credential" 对象?
How to create "credential" object needed by Firebase web user.reauthenticateWithCredential() method?
new docs 中的(不清楚的)示例:
var user = firebase.auth().currentUser;
var credential;
// Prompt the user to re-provide their sign-in credentials
user.reauthenticateWithCredential(credential).then(function() {
我应该如何创建这个 credential
对象?
我试过:
reauthenticateWithCredential(email, password)
(如登录方式)
reauthenticateWithCredential({ email, password })
(文档只提到一个参数)
运气不好:(
PS:我不计算在新文档中搜索相关信息所浪费的时间...我非常想念精彩的 firebase.com 文档,但是想要为 firebase.storage...
切换到 v3 或更高版本
我同意文档对此不是很清楚。但是更深入地查看 API 参考文献,我发现 firebase.auth.AuthCredential and this 并且我想您应该将其传递给 reauthenticate()
.
我在这里猜测,但我会开始尝试记录 firebase.auth()
以查看那里是否有任何 credential
对象。
我想它看起来像下面这样:
user.reauthenticate(firebase.auth().credential).then(function() {
我设法让它工作了,应该更新文档以包含这个,以供那些不想在详尽但难以阅读的 API 参考上花费太多时间的人使用。
Firebase 8.x
凭据对象是这样创建的:
const user = firebase.auth().currentUser;
const credential = firebase.auth.EmailAuthProvider.credential(
user.email,
userProvidedPassword
);
// Now you can use that to reauthenticate
user.reauthenticateWithCredential(credential);
Firebase 9.x
(感谢@Dako Junior 的回答,我在这里添加是为了详尽无遗)
import {
EmailAuthProvider,
getAuth,
reauthenticateWithCredential,
} from 'firebase/auth'
const auth = getAuth()
const credential = EmailAuthProvider.credential(
auth.currentUser.email,
userProvidedPassword
)
const result = await reauthenticateWithCredential(
auth.currentUser,
credential
)
// User successfully reauthenticated. New ID tokens should be valid.
备注
有人问 userProvidedPassword
,是否是第一次登录时存储的某种变量。不是,你应该打开一个新的 dialog/page 并输入密码,然后用户将再次输入他们的密码 。
我坚持认为您绝不能尝试通过以明文形式存储用户密码来解决此问题。这是应用程序的 正常 功能。例如,在 GMail 中,有时您的会话会过期,或者怀疑被黑客入侵、您更改位置等。GMail 会再次询问您的密码。这是重新认证。
这种情况不会经常发生,但使用 Firebase 的应用应该支持它,否则用户会卡在某个时候。
final FirebaseUser fireBaseUser = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider.getCredential(fireBaseUser.getEmail(), storedPassword);
fireBaseUser.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> reAuthenticateTask) {
if (!reAuthenticateTask.isSuccessful())
...
}
});
完整答案 - 您可以使用以下内容:
var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
user.email,
'yourpassword'
);
user.reauthenticateWithCredential(credentials);
请注意 reauthenticateWithCredential
是 reauthenticate()
的更新版本
由于两个发布的答案都已弃用,现在方法有一个小的变化,
val user = auth.currentUser
user?.let { _user ->
val credentials = EmailAuthProvider.getCredential(
_user.email!!,
"userPassword"
)
_user.reauthenticate(credentials).addOnCompleteListener { _reauthenticateTask ->
}
有多种方法可以重新认证。见参考文献:https://firebase.google.com/docs/reference/js/firebase.User
firebase
.auth()
.currentUser.reauthenticateWithPopup(new firebase.auth.GoogleAuthProvider())
.then((UserCredential) => {
console.log("re-outh", UserCredential);
});
如果您的应用允许多种身份验证方法,您可能需要首先找出使用的是什么提供程序。您可以通过查看 firebase.auth().currentUser.providerData
数组来做到这一点。
使用新的 Firebase 版本 9。*
import {
EmailAuthProvider,
getAuth,
reauthenticateWithCredential,
} from "firebase/auth";
const auth = getAuth();
let credential = EmailAuthProvider.credential(
auth.currentUser.email,
password
);
reauthenticateWithCredential(auth.currentUser, credential)
.then(result => {
// User successfully reauthenticated. New ID tokens should be valid.
})
new docs 中的(不清楚的)示例:
var user = firebase.auth().currentUser;
var credential;
// Prompt the user to re-provide their sign-in credentials
user.reauthenticateWithCredential(credential).then(function() {
我应该如何创建这个 credential
对象?
我试过:
reauthenticateWithCredential(email, password)
(如登录方式)reauthenticateWithCredential({ email, password })
(文档只提到一个参数)
运气不好:(
PS:我不计算在新文档中搜索相关信息所浪费的时间...我非常想念精彩的 firebase.com 文档,但是想要为 firebase.storage...
切换到 v3 或更高版本我同意文档对此不是很清楚。但是更深入地查看 API 参考文献,我发现 firebase.auth.AuthCredential and this 并且我想您应该将其传递给 reauthenticate()
.
我在这里猜测,但我会开始尝试记录 firebase.auth()
以查看那里是否有任何 credential
对象。
我想它看起来像下面这样:
user.reauthenticate(firebase.auth().credential).then(function() {
我设法让它工作了,应该更新文档以包含这个,以供那些不想在详尽但难以阅读的 API 参考上花费太多时间的人使用。
Firebase 8.x
凭据对象是这样创建的:
const user = firebase.auth().currentUser;
const credential = firebase.auth.EmailAuthProvider.credential(
user.email,
userProvidedPassword
);
// Now you can use that to reauthenticate
user.reauthenticateWithCredential(credential);
Firebase 9.x
(感谢@Dako Junior 的回答,我在这里添加是为了详尽无遗)
import {
EmailAuthProvider,
getAuth,
reauthenticateWithCredential,
} from 'firebase/auth'
const auth = getAuth()
const credential = EmailAuthProvider.credential(
auth.currentUser.email,
userProvidedPassword
)
const result = await reauthenticateWithCredential(
auth.currentUser,
credential
)
// User successfully reauthenticated. New ID tokens should be valid.
备注
有人问 userProvidedPassword
,是否是第一次登录时存储的某种变量。不是,你应该打开一个新的 dialog/page 并输入密码,然后用户将再次输入他们的密码 。
我坚持认为您绝不能尝试通过以明文形式存储用户密码来解决此问题。这是应用程序的 正常 功能。例如,在 GMail 中,有时您的会话会过期,或者怀疑被黑客入侵、您更改位置等。GMail 会再次询问您的密码。这是重新认证。
这种情况不会经常发生,但使用 Firebase 的应用应该支持它,否则用户会卡在某个时候。
final FirebaseUser fireBaseUser = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider.getCredential(fireBaseUser.getEmail(), storedPassword);
fireBaseUser.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> reAuthenticateTask) {
if (!reAuthenticateTask.isSuccessful())
...
}
});
完整答案 - 您可以使用以下内容:
var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
user.email,
'yourpassword'
);
user.reauthenticateWithCredential(credentials);
请注意 reauthenticateWithCredential
是 reauthenticate()
由于两个发布的答案都已弃用,现在方法有一个小的变化,
val user = auth.currentUser
user?.let { _user ->
val credentials = EmailAuthProvider.getCredential(
_user.email!!,
"userPassword"
)
_user.reauthenticate(credentials).addOnCompleteListener { _reauthenticateTask ->
}
有多种方法可以重新认证。见参考文献:https://firebase.google.com/docs/reference/js/firebase.User
firebase
.auth()
.currentUser.reauthenticateWithPopup(new firebase.auth.GoogleAuthProvider())
.then((UserCredential) => {
console.log("re-outh", UserCredential);
});
如果您的应用允许多种身份验证方法,您可能需要首先找出使用的是什么提供程序。您可以通过查看 firebase.auth().currentUser.providerData
数组来做到这一点。
使用新的 Firebase 版本 9。*
import {
EmailAuthProvider,
getAuth,
reauthenticateWithCredential,
} from "firebase/auth";
const auth = getAuth();
let credential = EmailAuthProvider.credential(
auth.currentUser.email,
password
);
reauthenticateWithCredential(auth.currentUser, credential)
.then(result => {
// User successfully reauthenticated. New ID tokens should be valid.
})