首页 > 解决方案 > Firebase 有什么方法可以同时退出平台网络和移动应用程序?

问题描述

我可以让用户同时退出网络和移动应用程序,这样一旦从网络应用程序退出,就无法再登录移动应用程序吗?

标签: firebasefirebase-authentication

解决方案


没有办法在不同的设备上注销某人,但您有几个选择,只需将一些元素放在一起。

您可以撤销用户刷新令牌,这意味着当令牌过期并且 sdk 去刷新它时它不能并且会注销用户。

// Revoke all refresh tokens for a specified user for whatever reason.
// Retrieve the timestamp of the revocation, in seconds since the epoch.
admin.auth().revokeRefreshTokens(uid)
    .then(() => {
      return admin.auth().getUser(uid);
    })
    .then((userRecord) => {
      return new Date(userRecord.tokensValidAfterTime).getTime() / 1000;
    })
    .then((timestamp) => {
      console.log("Tokens revoked at: ", timestamp);
  });

您还可以在 firebase 实时数据库中放置一个标志,然后当他们在其他设备上重新打开应用程序时,如果登录,他们可以读取该标志并在客户端上注销。此外,如果他们打开了网络/移动应用程序,如果他们一直在监听该标志,您可以以这种方式将他们注销。

var logoutRef = firebase.database().ref('userLogoutRef/' + userUid);
logoutRef.on('value', function(snapshot) {
  if (snapshot.val() === true) {
      firebase.auth().signOut()
  }
});

您只想确保在后续登录时删除此标志,以便用户可以登录。


推荐阅读