【问题标题】:How to check if a user is disabled or not in Firebase when authenticating?身份验证时如何检查用户是否在 Firebase 中被禁用?
【发布时间】:2020-07-26 19:00:32
【问题描述】:

有趣的是,我找不到一个地方说明如何检查用户是否被禁用以阻止他登录。

正如您在文档中看到的那样,User object 不包含任何名为 disabled 的属性,那么我想如何验证尝试登录的用户是否被禁用?

我的登录代码:

firebase.auth().signInWithEmailAndPassword(login_email, login_password)
        .then((onfulfilled, onrejected) => {
            console.log(onfulfilled); //no disabled property is printed
             })

【问题讨论】:

    标签: firebase react-native firebase-authentication


    【解决方案1】:

    您可能缺少一个 catch 块。尝试登录时,Firebase 应该返回错误(请参阅 firebase 文档中的 here)。

    auth()
      .createUserWithEmailAndPassword(login_email, login_password)
      .then(() => {
        console.log('User account created & signed in!');
      })
      .catch(error => {
        if (error.code === 'auth/user-disabled') {
          console.log('Sign-in for this email address is disabled!');
        }
        console.error(error);
      });
    

    【讨论】:

      【解决方案2】:

      如文档中所述,如果用户被禁用,signInWithEmailAndPassword() 方法将失败并出现带有auth/user-disabled 代码的错误。您将能够通过catch() 检测到这种情况,如下所示:

      firebase.auth().signInWithEmailAndPassword(login_email, login_password)
              .then(userCredential => {
                  console.log(onfulfilled); //no disabled property is printed
              })
              .catch(error => {
                 if (error.code === 'auth/user-disabled') {
                   // User is disabled
                 }
               })
      

      【讨论】:

        猜你喜欢
        • 2018-12-28
        • 1970-01-01
        • 2021-12-26
        • 2019-01-20
        • 2017-05-15
        • 2023-03-04
        • 1970-01-01
        • 2018-11-11
        相关资源
        最近更新 更多