javascript - Restrict access to a page if not logging in with Firebase Authentification + retrieve data -


i have login page following code (taken firebase sample auth app):

    function initapp() {   // listening auth state changes.   // [start authstatelistener]   firebase.auth().onauthstatechanged(function(user) {     // [start_exclude silent]     document.getelementbyid('quickstart-verify-email').disabled = true;     // [end_exclude]     if (user) {         window.location = 'index.html';       // user signed in.       var displayname = user.displayname;       var email = user.email;       var emailverified = user.emailverified;       var photourl = user.photourl;       var isanonymous = user.isanonymous;       var uid = user.uid;       var providerdata = user.providerdata;       // [start_exclude]       document.getelementbyid('quickstart-sign-in-status').textcontent = 'signed in';       document.getelementbyid('quickstart-sign-in').textcontent = 'sign out';       document.getelementbyid('quickstart-account-details').textcontent = json.stringify(user, null, '  ');       if (!emailverified) {         document.getelementbyid('quickstart-verify-email').disabled = false;       }       // [end_exclude]     } else {       // user signed out.       // [start_exclude]       document.getelementbyid('quickstart-sign-in-status').textcontent = 'signed out';       document.getelementbyid('quickstart-sign-in').textcontent = 'sign in';       document.getelementbyid('quickstart-account-details').textcontent = 'null';       // [end_exclude]     }     // [start_exclude silent]     document.getelementbyid('quickstart-sign-in').disabled = false;     // [end_exclude]   });   // [end authstatelistener]   document.getelementbyid('quickstart-sign-in').addeventlistener('click', togglesignin, false);   document.getelementbyid('quickstart-sign-up').addeventlistener('click', handlesignup, false);   document.getelementbyid('quickstart-verify-email').addeventlistener('click', sendemailverification, false);   document.getelementbyid('quickstart-password-reset').addeventlistener('click', sendpasswordreset, false); } window.onload = function() {   initapp(); }; 

i added line window.location = 'index.html', in order redirect user index.html when logging in.

now on index.html file, have following js code:

     function initapp() {            firebase.auth().onauthstatechanged(function(user) {              if (user) {        // user signed in.       var displayname = user.displayname;       var email = user.email;       var emailverified = user.emailverified;       var photourl = user.photourl;       var isanonymous = user.isanonymous;       var uid = user.uid;       //var uid = bcbadd1qjqhovbdg0qcdilq5emx1;       var providerdata = user.providerdata;       }      else {      window.location = 'login.html';     }   }); } window.onload = function() {   initapp(); };   window.onload = function() {       document.getelementbyid('sign-out').onclick = function togglesignout() {   firebase.auth().signout().then(function() {   console.log('signed out');     window.location = 'login.html'; }, function(error) {   console.error('sign out error', error); }); } }     var database = firebase.database();     database.ref(`user_profiles/${uid}/name`).once('value').then(photossnap => {     var username = photossnap.val();     $('#username').html(username);         }); 

there sign-out button works. issue page should accessible logged in, present code can access it, logged in or not. thought part make it:

else { window.location = 'login.html';

what shall implement restrict access?

moreover, retrieve userid current logged in user. last part of above code should make it, doesn't retrieve anything. i'm using uid variable, set above var uid = user.uid;


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -