Daniel C Daniel C - 1 year ago 95
Javascript Question

Enforce Firebase Authentication on Page

I am using Firebase Authentication with Firebase UI to protect pages by requiring a user to be logged in.

Somehow in the documentation I don't see what script we need to use on the top of each of the pages, in order to determine if user is logged in (else, redirect to login page).

In simple PHP, I can do the following:



However, I don't see what is needed to be included in the Firebase JS authentication (and/or using Firebase UI).

Sorry if this seems obvious, but I've spent a long time trying to find a solution with no luck.

Answer Source

In JavaScript you can detect whether the user is signed in with:

var user = firebase.auth().currentUser;

if (user) {
  // User is signed in.
} else {
  // No user is signed in.

See the Firebase documentation on determining what user is signed in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download