Framework7: Login redirect

2mas picture 2mas · May 11, 2016 · Viewed 8.6k times · Source

When user visits login.html page, localStorage is used to check if a user is logged in. The page should redirect to profile.html and display notofication message. The message is displayed, but the page (login.html) is the same..

if( localStorage.user_login ) {
    mainView.router.loadPage({url:'profile.html', ignoreCache:true, reload:true });

myApp.addNotification( {
        message: 'Welcome '+ localStorage.user_username +'!'
      } );
}

How can i make the page redirect if the user is logged in?

Answer

Christian David Zapanta picture Christian David Zapanta · Oct 20, 2016

put this before myApp framework7 initialization.

    $$(document).on('pageInit', function (e) {
       var page = e.detail.page;
       if (page.name === 'index') {
         try{
           var storedData = window.localStorage['f7form-'+ 'idofyourloginform'];
          if(storedData) {
              //do your ajax login request here
              // if successful do your login redirect  
                 mainView.router.loadPage({url:'profile.html', ignoreCache:true, reload:true });
           }
       }
     );