ken
ken

Reputation: 8993

Client-side check if authenticated in firebase

Is there a way in Firebase to check if you are authenticated? I could request a resource and catch the a 401 error but I'd like to avoid making the request if I already have authenticated.

Upvotes: 0

Views: 1152

Answers (1)

David East
David East

Reputation: 32604

Each Firebase reference has a way of getting the currently authenticated user.

For JavaScript you can call .getAuth().

var ref = new Firebase("<my-firebase-app>");
var authData = ref.getAuth();
if (authData !== null) {
   // you're logged in
}

You can also listen for authenticate state change in realtime with .onAuth():

var ref = new Firebase("<my-firebase-app>");
ref.onAuth(function(authData) {
  if (authData !== null) {
    // you're logged in
  }
});

Upvotes: 3

Related Questions