Redirect based on token from API

Hey Hey,

i wanted to get some advice on how to tackle this issue.

So i have a login / register form which interacts with my PHP API, i can register a user and also log a user in.

When i login someone in, a token gets generated on the API and then stored to localstorage.

Obviously when someone logs in, i want them to stay logged in unless they press the logout button.

Can anyone give me some advice on how i would accomplish this.

Im using Ionic 2 beta 11


When the user reopens the app go to the login screen (or some other intermediary screen), if there is a token in local storage automatically run a check against your server for the token + username, if the session is valid redirect (using setRoot) to the main app page, otherwise stay on the login page.

Hey @joshmorony

I was thinking around those lines.

In my case, it would be email address + token, but would i put the check logic in the constructor of the app.ts file?

Sorry about the newness of the question, im just getting started with all this Angular 2 and Ionic stuff

Set the root page to the login page, or some other page if you prefer, and handle the logic for checking then changing the root page there. So in login.ts not app.ts - you might be tempted to do the check in app.ts and set the initial root page according to the result, but asynchronous calls don’t play nicely with setting the root page that way.

Ahhhh, that makes sense … so if i had a ‘profile’ page that can only be accessed if the user logged in, would i need to secure that page as well some how, or would the login check take care of any redirects?