3

Looking to get some help with a little bit of code to include on a page I want to be only accessed by logged in users and if a user is not logged in they can be redirected to the login page or log in on the same locked page.

Cheers

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Tal Amos
  • 123
  • 1
  • 2
  • 4
  • Where is the web page hosted? If you're [using Firebase Hosting, there is no way to make the web page itself inaccessible](http://stackoverflow.com/questions/27212004/can-firebase-hosting-restrict-access-to-resources/27213823#27213823). All assets on Firebase Hosting are public. If your web page is accessing the Firebase Database, you can [use security rules](https://firebase.google.com/docs/database/security/) to control access to that data by specific users. – Frank van Puffelen Aug 19 '16 at 14:53
  • I was planning to host with firebase but if it's not possible I can host elsewhere. This is for Web (for those who didn't read the title :) ) – Tal Amos Aug 20 '16 at 03:22

1 Answers1

3

Always use onAuthStateChanged() to keep track of the user's login or logout status.

//Handle Account Status
firebase.auth().onAuthStateChanged(user => {
  if(!user) {
    window.location = 'login.html'; //If User is not logged in, redirect to login page
  }
});

The above code will make sure that if a user is not signed in, they will be redirected to login page and if they are signed in, they will stay on the current page.

gegobyte
  • 4,945
  • 10
  • 46
  • 76