Search code examples
javascriptjquerysails.js

Check if user has logged in on client side


Is there a safe way to check if the user has logged into the application rather than checking if "sid" cookie exists in user's machine ?

I want to allow the user to proceed on certain links on a page only if they have logged in.

I do the login validation on the server side but want to avoid the request trip.

Pure JS or JQuery solution would be appreciated.

Thank you.


Solution

  • Please try this

    Put this code after user first log in

    jQuery(window).load(function() {
      sessionStorage.setItem('status','loggedIn') 
    });
    

    When ever user clicks a link you can check like

    if (sessionStorage.getItem('status') != null))
        //redirect to page
    }
    else{
        //show validation message
    }