Coder1000 Coder1000 - 2 months ago 27
Javascript Question

How to check wether the user is logged in with Firebase on the Client Side?


Thank you for your help ! You helped me pinpoint the problem and showed me the solution I was looking for: Accessing Express.js local variables in client side JavaScript



$('.UpvoteButton').click(function () {
if (authdata == null) {
return res.redirect('/users/login');
else {
var $this = $(this);
var $other = $('.DownvoteButton');
if ($this.hasClass("on")) {
} else if (!$this.hasClass('on') && $other.hasClass("on")) {
} else {


//Global vars
app.use(function(req, res, next) {
res.locals.authdata = firebase.auth().currentUser;


When I click, nothing happens. It worked before when I didn't have the if-else statement and the redirect statement.

N.B.: This is inside an EJS file. I am using Node.js.


What have I done wrong ?


As stated, you're trying to access a server-sided variable on the client side. Check this out: Accessing EJS variable in Javascript logic.

Essentially, you'll want to declare the variable on the client side using:

    var authdata = <%= authdata %>            

Then, you can add your code. Remember to change the redirect to something that is recognized by the client ie. window.location.href = "/users/login";