如何创建 Firebase web 用户所需的“凭据”对象,以便使用 reauthenticateWithCredential() 方法?

122

新文档中的(不清楚的)示例在此处

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或更高版本...

7个回答

242

我设法让它工作了,文档应该更新,以包括那些不想在详尽但难以阅读的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是否是第一次登录时存储的某种变量。它不是,您应该打开一个新的对话框/页面并输入密码,用户将再次输入他们的密码。

我坚决认为,您绝不能尝试通过明文存储用户密码来解决此问题。这是应用程序的正常功能。例如,在GMail中,有时会话过期,出现黑客疑虑,更改位置等情况。GMail会再次要求输入密码进行重新验证。

这种情况不会经常发生,但使用Firebase的应用程序应支持它,否则用户将在某个时候被卡住。


5
很高兴听到您找到了解决方案!我会添加一条注释来更新/澄清文档。请记住,每个页面上也有一个反馈按钮,专门用于此目的。 :-) - Frank van Puffelen
50
三年过去了,但文件仍需要更新!感谢您节省了我的时间。 - Ramtin Soltani
24
四年后...但这份文档仍然是“详尽但难以阅读”。 - Nik
1
@FotiosTsakiris,你绝对不应该尝试通过以明文形式存储用户密码来解决此问题,这似乎是应用程序的正常功能。例如,在GMail中,有时会话过期,或者存在黑客嫌疑,您更改位置等。GMail会再次要求输入密码。这是重新验证。虽然不会经常发生,但使用Firebase的应用程序应支持它,否则用户将在某些时候被卡住。为了清晰起见,将其添加到答案中。 - Pandaiolo
1
5年半后... - stevehs17
显示剩余11条评论

40

完整答案 - 您可以使用以下内容:

var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credentials);
请注意,reauthenticateWithCredentialreauthenticate()的更新版本。

4
谢谢您添加了被采纳答案中缺失的第三行。 - tim-phillips
1
@Oliver 密码应该是与该电子邮件已经存在的那个。 - maudulus
不确定是否是@Sandokan,可以在此处查看:https://firebase.google.com/docs/reference/js/firebase.User#reauthenticatewithcredential-您在哪里看到它已过时了? - maudulus
@Sandokan 我认为你错了 - reauthenticateAndRetrieveDataWithCredential 已经被弃用了:https://firebase.google.com/docs/reference/js/firebase.User#reauthenticateandretrievedatawithcredential - maudulus
@maudulus。 是的,你说得对。 在我的情况下,这是firebase版本问题。我正在使用firebase5.7。 firebase.User.prototype.reauthenticateWithCredential已被弃用。请改用firebase.User.prototype.reauthenticateAndRetrieveDataWithCredential。 - Sandokan

6

使用新的 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.
    })

5

有多种方法可以重新进行身份验证。参见参考文献: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 数组来实现此目的。


0

我同意文档对此并不十分清晰。但是在API参考中深入了解后,我发现了firebase.auth.AuthCredentialthis,我猜你应该将它传递给reauthenticate()

我猜测一下,我会尝试记录firebase.auth(),看看是否有任何credential对象。

我想它看起来应该像以下内容:

user.reauthenticate(firebase.auth().credential).then(function() {

我已经成功让它工作了,我正在撰写答案。你在回答中忘记了电子邮件和密码 :) - Pandaiolo

0
现在方法有一点小改变,因为两个发布的答案都已经过时了。
    val user = auth.currentUser
    user?.let { _user ->
        val credentials = EmailAuthProvider.getCredential(
            _user.email!!,
            "userPassword"
        )
        _user.reauthenticate(credentials).addOnCompleteListener { _reauthenticateTask ->
  }

-2
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())
               ...
     }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接