【问题标题】:How to logout the user using Firebase Admin SDK?如何使用 Firebase Admin SDK 注销用户?
【发布时间】:2018-10-03 15:36:30
【问题描述】:

所以,我使用 Firebase Admin SDK 创建了一个云功能。该功能的目的是禁用用户,在成功禁用它后,我希望该用户从我的应用程序中注销。我已禁用用户,但不知道如何注销用户。

我想知道是否有任何变通方法可以实现这一点?

【问题讨论】:

    标签: firebase google-cloud-functions firebase-admin


    【解决方案1】:

    登录到您的应用的用户拥有一个有效期最长为一小时的 ID 令牌。一旦创建了该令牌,就无法撤销它。

    处理您的用例的典型方法是在您禁用用户帐户后在服务器端数据库中标记该用户,然后在任何操作中检查该标记。

    例如,如果您使用 Firebase 实时数据库,并使用 Node.js 禁用用户,那么在数据库中标记用户的代码可能如下所示:

    // Disable the user in Firebase Authentication to prevent them from signing in or refreshing their token
    admin.auth().updateUser(uid, {
      disabled: true
    }).then(function() {
      // Flag the user as disabled in the database, so that we can prevent their reads/writes
      firebase.database().ref("blacklist").child(uid).set(true);
    });
    

    然后您可以在服务器端安全规则中使用以下内容进行检查:

    {
      "rules": {
        ".read": "auth.uid !== null && !root.child('blacklist').child(auth.uid).exists()"
      }
    }
    

    此规则允许所有已登录 (auth.uid !== null) 的用户对数据库进行完全读取访问,但阻止已标记的用户 (!root.child('blacklist').child(auth.uid).exists())。

    有关此方法的(甚至)更详细的示例,请参阅documentation on session management

    【讨论】:

    • 我不想禁用用户,我只想撤销用户令牌,这样他就无法访问 API。有没有办法管理它
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2017-04-22
    • 2019-04-26
    • 1970-01-01
    • 2019-01-27
    • 2019-06-14
    • 2016-09-19
    相关资源
    最近更新 更多