我正在尝试建立一个小网页,其中登录是由Firebase谷歌身份验证控制,并与个人资料页弹出。显示个人资料页面的安全和首选方式是什么?
目前,我正在使用onAuthStateChanged来操作特定的div,它在用户登录时保存配置文件数据。如果用户没有登录,我将使用removeChild()方法从DOM中删除该div,当用户登录时,appendChild()会添加回该div。
发布于 2020-07-27 18:43:52
假设您使用的是firebase的原生firebase.auth().onAuthStateChanged函数
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});以及用于检查用户当前是否已登录的firebase.auth().currentUser。
在这种情况下,完全可以使用removeChild和appendChild,并且它们不存在任何安全威胁,就像用户没有登录一样,在页面刷新后,所有信息都将消失。
这是一个小的firebase应用程序,它显示了当关闭到firebase的连接并使用removeChild时,appendChild将停止工作,因为firebase断开连接,从而证明了使用它是安全的。
https://jsfiddle.net/vh9xay6e/
请注意,在本例中,我没有测试任何身份验证,只是在removeChild和appendChild中使用firebase。
你可以看到,一旦与Firebase的连接结束,前端的任何事情都不能改变这一点。
发布于 2020-07-30 21:15:59
使用onAuthStateChanged方法,我们可以在状态更改时采取行动(登录或注销)
举个例子:
firebase.auth().onAuthStateChanged(user=>{
if(user){
document.getElementById("id").classList.remove('hide')
//this will populate your class which associate with above id.
} else{
document.getElementById("id_of_your_div").classList.add('hide')
}
})我认为在应用程序中使用基于firebase身份验证状态更改的removeChild和appendChild方法是可以的。
发布于 2020-07-30 21:34:13
尝试通过在函数中使用var userCurrent = firebase.auth().currentUser;来连接您的代码。
注意:请确保您首先登录(因为这是一个异步操作),然后将用户数据更新为:
var authenticationRef = firebase.auth();
authenticationRef.onAuthStateChanged(function(user) {
if (user) {
console.log('onAuthStateChanged : '+user.displayName);
_updateUser();
} else {
console.log('Not login');
}
});
fucntion _updateUser(){
var userCurrent = firebase.auth().currentUser;
userCurrent.updateProfile({
displayName: "Karthik.S",
}).then(function() {
var displayName = userCurrent.displayName;
}, function(error) {
});
}https://stackoverflow.com/questions/55611422
复制相似问题