Login page check

Goodmorning everyone,
as you can exit routes when the user is not logged in.
I modified the kitchen-sink example but it does not work.

This is the code:

var routes = [
...
...
   // Components
   {
     path: '/ accordion /',
    
async (routeTo, routeFrom, resolve, reject) {
if (sessionStorage! == undefined) {
resolve ({url: './pages/accordion.html'})
} else {
resolve ({url: './pages/login-screen-page.html'})
}
}
   },

...
...

];

Thanks for help.

Michele.

this is illegal:
if (sessionStorage! == undefined)
it should be:
if (sessionStorage !== undefined)
or better:
if (sessionStorage.length)

{
  path:'/login/',
  componentUrl:'./pages/login-screen-page.html'
},
{
  path:'/accordion/',
  url:'./pages/accordion.html',
},
{
  path:'/check/',
  redirect:function(route,resolve,reject){
    resolve(sessionStorage.length?'/accordion/':'/login/');
  }
}

Thanks for the reply.
I made the change, but still does not go to the correct page after the test. In practice, if the user is not logged in he never goes to the instruction: resolve ({url: ‘./pages/login-screen-page.html’})

I’ve also tried this, but I can not do what I want.
In practice, using the kitchen-sink (v.2.0.5) example I would like the page / accordion / or any other one open only if you have logged in, otherwise it must always ask for login. Maybe I do not have the right level yet to fully understand this framework.

I apologize for my terrible English.

Thank you for your interest.

Michele.

sorry
that will do it
i just checked
it works

{
  path:'/accordion/',
  url:sessionStorage.length?'./pages/accordion.html','./pages/buttons.html',
},

I made this change:

// Components
{
// path: ‘/accordion/’,
// url: ‘./pages/accordion.html’,
path:’/accordion/’,
componentUrl:sessionStorage.length?’./pages/accordion.html’:’./pages/login-screen-page.html’,
},

but after logging in if I click on / accordion / continue to open the login even if the sessionStorage is enhanced.

I checked via: console.log (sessionStorage.user);

Grazie. Michele.

now i get you
sorry for everything
you need to use the option that you provide

{
  path:'/accordion/',
  async(routeTo, routeFrom, resole, reject) {
    if (sessionStorage.length) {
      resolve({url:'./pages/accordion.html'})
    } else {
      resolve({componentUrl:'./pages/login-screen-page.html'})
    }
  }
}

Hurray now works. I had already tried the async function, but without result.
Maybe because I always used componentUrl.
In any case, thank you very much for your help.
Now you need a little imagination to make a good application.

Michele Lo Monaco.

Good morning,
I continued my tests by adding a php page with the session in order to differentiate a result, but until I force a page refresh I never see the effect of the session variable: $ _SESSION [“mioutente”]

I put the project in my web space.

kitchen-sink.rar (right click)

The page in the menu is: Pagina Test

Can you give me a tip.

Thank you.

Michele.

sessionStorage doesn’t related to $_SESSION, sessionStorage is the client only

Thank you.
So all login must be managed by client?

There is an example online that we can consult.

Michele.

No, there is a ton of ways to handle it, in most cases when auth is server side, then usually on app launch you can do a request to server to verify that you are logged in.

1 Like

Or, with every request you can send additional auth header with token or something else

1 Like