Daniel C Daniel C - 1 month ago 25
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:

require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}


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

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.