【问题标题】:Firebase Auth - Trouble Reauthenticating User Using GoogleAuthProviderFirebase 身份验证 - 使用 GoogleAuthProvider 重新验证用户时遇到问题
【发布时间】:2019-03-15 02:45:47
【问题描述】:

根据文档,我需要重新验证尝试删除其帐户但有一段时间未登录的用户。这是doc(最底部):

文档

如果您执行了其中一项操作,并且用户登录时间过长 以前,该操作因错误而失败。当这件事发生时, 通过从 用户并将凭据传递给 reauthenticateWithCredential

var user = firebase.auth().currentUser;
var credential;

// Prompt the user to re-provide their sign-in credentials

user.reauthenticateAndRetrieveDataWithCredential(credential).then(function() {
  // User re-authenticated.
})

凭证?

我的问题在于从用户那里获取新的登录凭据部分。凭证变量应该具有什么值?根据Puff's comment here,它应该去:

var credential = firebase.auth.EmailAuthProvider.credential(email, password);

我的问题:电子邮件和密码来自哪里?由于我使用的是 GoogleSignInAuth,因此可以使用 firebase.auth().currentUser.email 检索电子邮件,因此它看起来像这样:

var user = firebase.auth().currentUser;
let credential = firebase.auth.GoogleAuthProvider.credential(firebase.auth().currentUser.email);

user.reauthenticateAndRetrieveDataWithCredential(credential).then(() => {
  // User re-authenticated.
});

但我收到一个错误:

{"error":{"code":400,[{"message":"无法解析谷歌 id_token:qmnofficial@gmail.com", "domain":"global","re​​ason":"invalid"}]}}

请帮忙。

【问题讨论】:

    标签: angular firebase firebase-authentication


    【解决方案1】:

    您的问题是您尝试使用电子邮件地址检索 google 凭据,这是不可能的。

    let credential = firebase.auth.GoogleAuthProvider.credential(firebase.auth().currentUser.email);
    

    根据文档,

    credential(idToken, accessToken) 返回 firebase.auth.OAuthCredential

    为 Google 创建凭据。 ID 令牌和访问权限中的至少一项 令牌是必需的。

    var credential = firebase.auth.GoogleAuthProvider.credential(id_token);
    

    var credential = firebase.auth.GoogleAuthProvider.credential(null,access_token);
    

    对于您的用例,这样做,

    firebase.auth().currentUser.reauthenticateWithPopup(
        new firebase.auth.GoogleAuthProvider()).then(function(userCredential) {
            // You can now delete the user:
            return firebase.auth().currentUser.delete();  
        }).catch(function(error) {
            // Credential mismatch or some other error.  
        }
    );
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 2020-10-13
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多