Firebase注销用户所有会话

时间:2019-02-15 07:09:14

标签: javascript ios swift firebase

我在我的iOS应用中使用Firebase身份验证。当用户使用Firebase登录我的应用程序然后注销该用户的所有其他设备(会话)时,Firebase中是否有任何方法?我可以使用Firebase admin SDK来做到这一点吗?

3 个答案:

答案 0 :(得分:1)

Firebase不提供此类功能。您需要自己进行管理。

这里是Firebase Doc,他们还没有提到与单用户登录有关的任何内容。

这是您可以做的-

Firebase数据库中的“用户”节点中(在其中保存用户的其他数据)中获取一个令牌,并在每次登录到应用程序时重新生成该令牌,将该令牌与已经登录的用户令牌匹配(将其保存在本地)在appDidBecomeActiveappDidFinishLaunching中,或者每次您使用Firebase执行任何操作时,或者可能处于固定的时间间隔。如果令牌不同,则手动注销用户,并引导用户进行身份验证屏幕。

答案 1 :(得分:1)

当我遇到此问题时,我使用云功能解决了该问题 请访问此链接以获取更多详细信息https://firebase.google.com/docs/auth/admin/manage-sessions#revoke_refresh_tokens

执行以下操作;

  1. 设置具有Firebase云功能的Web服务器(如果不存在)
  2. 使用管理员APK(这是​​此方法唯一可行的方法)-[访问此链接]( (https://firebase.google.com/docs/admin/setup#initialize_the_sdk)。
  3. 创建一个可以接收uid并​​撤销当前会话的api,如上面的第一个链接所述
  admin.auth().revokeRefreshTokens(uid)
    .then(() => {
      return admin.auth().getUser(uid);
    })
    .then((userRecord) => {
      return new Date(userRecord.tokensValidAfterTime).getTime() / 1000;
    })
    .then((timestamp) => {
      //return valid response to ios app to continue the user's login process
  });

Voila用户已注销。我希望这能为解决问题提供见识

答案 2 :(得分:0)

我所做的是:

在 Firestore 中创建名为“activeSessions”的集合。用户电子邮件作为对象的 id 和“activeID”字段用于保存最近的会话 id。

登录页面代码:

每次用户登录时为用户会话生成 ID。 将此id添加到localstorage(每次添加前都应该清理)。 将“activeID”替换为“activeSessions”集合中生成的 ID 和当前用户电子邮件。

function addToActiveSession() {
  var sesID = gen();

  var db = firebase.firestore();
  localStorage.setItem('userID', sesID);
  db.collection("activeSessions").doc(firebase.auth().currentUser.email).set({
    activeID: sesID
  }).catch(function (error) {
      console.error("Error writing document: ", error);
    });

}
function gen() {
  var buf = new Uint8Array(1);
  window.crypto.getRandomValues(buf);
  return buf[0];
}
function signin(){
 firebase.auth().signInWithEmailAndPassword(email, password).then(function (user) {
      
      localStorage.clear();
      addToActiveSession();
      }
    }), function (error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode === 'auth/wrong-password') {
        alert('wrong pass');
      } else {
        alert(errorMessage);
      }
      console.log(error);
    };
}

然后我在每个页面上检查本地存储中的 id 会话是否与 firestore 中的“activeID”相同,如果不是,则注销。

function checkSession(){
  
  var db = firebase.firestore();
  var docRef = db.collection("activeSessions").doc(firebase.auth().currentUser.email);
        docRef.get().then(function (doc) {
          alert(doc.data().activeID);
          alert(localStorage.getItem('userID'));
          if (doc.data().activeID != localStorage.getItem('userID')) {
            alert("bie bie");
            firebase.auth().signOut().then(() => {
        
              window.location.href = "signin.html";
         }).catch((error) => {
           // An error happened.
         });
            window.location.href = "accountone.html";
          } else{alert("vse ok");}
        }).catch(function (error) {
          console.log("Error getting document:", error);
        });
}

PS:必须刷新窗口才能注销非活动会话。

相关问题