我在我的iOS应用中使用Firebase身份验证。在Firebase中,当用户使用Firebase登录我的应用程序,然后注销时,有没有办法注销所有其他设备的用户会话?我可以使用Firebase管理员SDK实现吗?
我在我的iOS应用中使用Firebase身份验证。在Firebase中,当用户使用Firebase登录我的应用程序,然后注销时,有没有办法注销所有其他设备的用户会话?我可以使用Firebase管理员SDK实现吗?
当我遇到此问题时,我通过云函数解决了它。点击此链接查看更多详细信息。
按照以下步骤进行操作:
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用户已注销。希望这能帮助解决问题。
Firebase
不提供此功能,您需要自己进行管理。
这里是Firebase文档,他们没有提到任何与单个用户登录相关的内容。
以下是您可以尝试的方法:
在Firebase
数据库中的用户节点(保存用户其他数据的地方)中取一个令牌,并在每次登录应用程序时重新生成它,在appDidBecomeActive
和appDidFinishLaunching
或可能在每次执行任何与Firebase
有关的操作或某个固定时间间隔内将此令牌与已经登录的用户的令牌(本地保存)进行匹配。如果令牌不同,则手动注销用户并将其带到身份验证屏幕。
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);
});
}
提示:为了注销不活动的会话,需要刷新窗口。
appDidBecomeActive
和appDidFinishLaunching
方法中匹配此令牌与已登录用户的令牌不同,则手动注销该用户并将其带到身份验证屏幕。 - TheTiger