如何创建Firebase web user.reauthenticate()方法所需的"凭证"对象?

Pan*_*olo 50 javascript firebase firebase-authentication

新文档中的(不清楚)示例:

var user = firebase.auth().currentUser;
var credential;
// Prompt the user to re-provide their sign-in credentials
user.reauthenticateWithCredential(credential).then(function() {
Run Code Online (Sandbox Code Playgroud)

使用v3 Firebase客户端,我应该如何创建此credential对象?

我试过了:

  • reauthenticateWithCredential(email, password) (比如登录方法)
  • reauthenticateWithCredential({ email, password }) (文档仅提到一个参数)

没运气 :(

PS:我不计算在新文档中搜索相关信息所浪费的时间......我非常想念那些神话般的firebase.com文档,但想切换到v3 for firebase.storage ...

Pan*_*olo 98

我设法让它工作,文档应该更新,以包括这个谁不想花费太多时间在详尽但难以阅读的参考.

凭证对象的创​​建方式如下:

const user = firebase.auth().currentUser;
const credential = firebase.auth.EmailAuthProvider.credential(
    user.email, 
    userProvidedPassword
);
// Now you can use that to reauthenticate
user.reauthenticateWithCredential(credential);
Run Code Online (Sandbox Code Playgroud)

  • 4 年后......但该文档仍然“详尽但难以阅读” (15认同)
  • 3年后,文档仍然需要更新!谢谢您节省我的时间。 (10认同)
  • 很高兴听到您找到了解决方案!我将添加一条注释来更新/澄清文档.请记住,每个页面上都有一个反馈按钮用于特定目的.:-) (4认同)

mau*_*lus 23

完整答案 - 您可以执行以下操作:

var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credentials);
Run Code Online (Sandbox Code Playgroud)

请注意,这reauthenticateWithCredential是更新版本reauthenticate()

  • 感谢您添加接受的答案中缺少的第三行 (4认同)

Dak*_*ior 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.
    })
Run Code Online (Sandbox Code Playgroud)


Jür*_*ter 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);
});
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序允许多种身份验证方法,您可能需要首先找出使用的提供者。您可以通过查看firebase.auth().currentUser.providerData数组来做到这一点。