简体   繁体   English

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

[英]How to create “credential” object needed by Firebase web user.reauthenticate() method?

var user = firebase.auth().currentUser;
var credential;
// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential).then(function() {

With the v3 Firebase client, how should I create this credential object for Google auth provider (Not email & Password). 使用v3 Firebase客户端,我应该如何为Google身份验证提供程序创建此凭据对象(不是电子邮件和密码)。

How to do it with email and password was answered here: Email&Password . 如何使用电子邮件和密码在这里回答: 电子邮件和密码

I've tried var credential = firebase.auth.GoogleAuthProvider.credential(); 我试过var credential = firebase.auth.GoogleAuthProvider.credential(); but according to the docs it's needs an "Google Id Token" which I have no idea how to get. 但根据文档,它需要一个“Google Id令牌”,我不知道如何获得。

You can create an AuthCredential by using firebase.auth.EmailAuthProvider.credential . 您可以创建一个AuthCredential使用firebase.auth.EmailAuthProvider.credential

Then you can reauthenticate with firebase.User.reauthenticateWithCredential . 然后,您可以使用firebase.User.reauthenticateWithCredential重新进行身份验证。

var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credential);

You can reauthenticate users that use GooglaAuth Provider without tokens and credentials using reauthenticateWithPopup(provider) or reauthenticateWithRedirect(provider) method: 您可以使用reauthenticateWithPopup(provider)reauthenticateWithRedirect(provider)方法重新验证使用GooglaAuth Provider而无需令牌和凭据的用户:

// Create provider as usual
function createGoogleProvider() {
  const provider = new auth.GoogleAuthProvider()
  provider.addScope('profile')
  provider.addScope('email')
  return provider
}
// Reauthenticate with popup:
user.reauthenticateWithPopup(createGoogleProvider()).then(function(result) {
  // The firebase.User instance:
  var user = result.user;
}, function(error) {
  // An error happened.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM