EDIT2:
CODE:
<script>
console.log("CURRENT USER:"+firebase.auth().currentUser);
var inElements = document.getElementsByClassName('authIn');
var outElements = document.getElementsByClassName('authOut');
if (firebase.auth().currentUser == null) {
for(var i=0; i < inElements.length; i++) {
inElements[i].style.display = "none";
}
for(var i=0; i < outElements.length; i++) {
outElements[i].style.display = "inline-block";
}
}
else {
for(var i=0; i < inElements.length; i++) {
inElements[i].style.display = "inline-block";
}
for(var i=0; i < outElements.length; i++) {
outElements[i].style.display = "none";
}
}
</script>
SITUATION:
I log in, firebase.auth().currentUser is null.
QUESTION:
Why and how do I fix it ?
LOGIN CODE:
<script>
firebase.auth().signInWithEmailAndPassword(email, password ).then( authData => {
//A LOT OF CODE FOR DIFFERENT SITUATIONS
},function(error) {
var errorCode = error.code;
var errorMessage = error.message;
localStorage.setItem('error_msg_local', "Unknown user or password");
window.location.href="/users/login";
console.log("Login Failed: ", error);
});
</script>
In the login code, I show different kinds of error messages depending on how the authentication went. There is also some code to manage verified and unverified accounts.
You need to set your authdata
variable so that it reflects whether the user is logged in or not.
You can run this code on initialization which creates an observer on the firebase Auth object. Anytime a logout/login change happens the authdata
variable will be updated accordingly..
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
authdata = user;
}
else {
authdata = null;
}
});
Alternatively if for some reason you don't want the observer approach you can simply set your authdata
like so:
authdata = firebase.auth().currentUser;
If the user is not logged in authdata will be set to null and your ejs
should update accordingly..
I'm assuming your ejs
is running on the client. If it's not then your ejs
template won't pick up on these changes.
You'll want to load the ejs
script client side and run your template from there.
See: EJS Client Side Support for reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.