【问题标题】:firebase reauthenticateAndRetrieveDataWithCredential facebook user credentialfirebase reauthenticateAndRetrieveDataWithCredential facebook 用户凭证
【发布时间】:2019-02-25 19:46:23
【问题描述】:

我正在尝试编写一个函数来重新验证使用 facebook 帐户登录的 firebase 用户。对于电子邮件,我从电子邮件和密码创建凭据,但 Facebook 没有为该用户存储密码。我试图在文档中找到这个但找不到

 let user = firebase.auth().currentUser;
 let credential = '';

    //User created with Email
    if(user.providerData[0].providerId === "password") {
      credential = firebase.auth.EmailAuthProvider.credential(
        user.email,
        this.state.password
      );
    }

   //User created with Facebook
   if(user.providerData[0].providerId === "facebook.com") {
      credential = firebase.auth.FacebookAuthProvider.credential(
         ? // <= User Email, Token 
       )
   } 
   user.reauthenticateAndRetrieveDataWithCredential(credential).then(function(){
         user.delete().then(function() {             
         }).catch(function(error) {
          console.log(error)
          }
      }).catch((error) => {
       console.log(error)
      }

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    根据 FacebookAuthProvider.credential() 签名,您将需要 OAuth 令牌/accessToken 来创建凭证。

    可以在用户登录时提取accessToken:

    firebase.auth().signInWithPopup(provider).then(function(result) {
      // This gives you a Facebook Access Token. You can use it to access the Facebook API.
      var token = result.credential.accessToken;
      // The signed-in user info.
      var user = result.user;
      // ...
    })
    

    该示例摘自https://firebase.google.com/docs/auth/web/facebook-login,您可以在其中找到有关此的更多信息。

    【讨论】:

      【解决方案2】:

      如果您想重新验证 Facebook 用户,只需使用 reauthenticateWithPopupreauthenticateWithRedirect,假设您使用 signInWithPopupsignInWithRedirect 使用 Facebook 登录用户。

      const provider = new firebase.auth.FacebookAuthProvider();
      firebase.auth().currentUser.reauthenticateWithPopup(provider)
        .then((userCredential) => {
          // User successfully reauthenticated.
        })
        .catch((error) => {
          // Error.
        });
      

      【讨论】:

        猜你喜欢
        • 2019-01-14
        • 2021-12-19
        • 2021-07-09
        • 2017-11-26
        • 2021-05-20
        • 2021-01-28
        • 2016-08-11
        • 2016-01-25
        • 2013-02-15
        相关资源
        最近更新 更多