# Redirect based on token from API

**URL:** <https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006>\
**Category:** ionic-v3\
**Created:** [August 9, 2016, 11:31am UTC](https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006 "2016-08-09T11:31:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris1981](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Chris1981](https://forum.ionicframework.com/u/Chris1981)\
**Post date:** [August 9, 2016, 11:31am UTC](https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006/1 "2016-08-09T11:31:01Z")

</div>

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

Nice!

---

<div class="post-metadata">

**Author:** ![joshmorony](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/joshmorony/32/117695_2.png) [@joshmorony](https://forum.ionicframework.com/u/joshmorony)\
**Post date:** [August 9, 2016, 12:14pm UTC](https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006/2 "2016-08-09T12:14:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chris1981](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Chris1981](https://forum.ionicframework.com/u/Chris1981)\
**Post date:** [August 9, 2016, 12:27pm UTC](https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006/3 "2016-08-09T12:27:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joshmorony](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/joshmorony/32/117695_2.png) [@joshmorony](https://forum.ionicframework.com/u/joshmorony)\
**Post date:** [August 9, 2016, 2:06pm UTC](https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006/4 "2016-08-09T14:06:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chris1981](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Chris1981](https://forum.ionicframework.com/u/Chris1981)\
**Post date:** [August 9, 2016, 10:55pm UTC](https://forum.ionicframework.com/t/redirect-based-on-token-from-api/60006/5 "2016-08-09T22:55:32Z")

</div>

> [@joshmorony](#):
>
> 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?
