如何使用Firebase Admin SDK注销用户?

时间:2018-10-03 15:36:30

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

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

我想知道是否有解决此问题的方法?

1 个答案:

答案 0 :(得分:3)

已登录到您的应用的用户具有一个有效期长达一个小时的ID令牌。创建该令牌后,将无法撤消它。

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

例如,如果您使用Firebase Realtime数据库,并使用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