Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic - How to store session token as globally (for app) accessible variable?

I know I can use localstorage or SQLite but I'm not sure how to exactly do that.

In my app, I'm getting the session token in the Login controller where I make post request to the server and in return get session token.

I'm not sure how to make this token globally accessible.

P.S: I'm very new to AngularJs.

like image 951
Vineonardo Avatar asked Aug 19 '15 06:08

Vineonardo


People also ask

How to store data inside an ionic app?

How to Store Data inside Ionic Apps 1 Setting up Ionic Storage. Ionic Storage is our go to package for easily managing data. ... 2 Working with Ionic Storage. Like in the HTTP lesson before it’s a good idea to maintain your logic in a provider and not directly inside the class of the ... 3 Using our Favorite Provider. ... 4 Next Steps. ...

What is ionic secure storage for iOS and Android?

For teams building mission-critical apps or requiring encryption support, Ionic Secure Storage is an official premium solution from the Ionic team that provides a cross-platform data storage system that works on iOS and Android. It makes it easy to build high performance, offline-ready Ionic apps across iOS, Android, and the web.

How to login to an Ionic 2 App?

In my Ionic 2 app i have a simple login which form is contained inside the right menu. You click on the right icon in the header - will appear the menu with the login form. The login code is inside the app.component and the login view is app.html. The successful login will set a boolean global variable - loginState - to true.

How does Auth connect work in ionic?

Using OpenID Connect authentication standards, Auth Connect provides all the infrastructure needed to set up login, logout, and token refresh in an Ionic app running on the web, iOS, and Android. For the best possible security and protection against data theft, it uses native system components, rather than an embedded browser.


1 Answers

in your controller once you get the token from the server

$scope.token = token;

you can say

localStorage.setItem("token", $scope.token);

then when you want to fetch the token (say in another controller) all you have to say is

$scope.token = localStorage.getItem("token");

Also if they re-open the app you can even check to see if they already have a token

if(localStorage.getItem("token") !== null && localStorage.getItem("token") !== ""){//go ahead and authenticate them without getting a new token.}

Also be aware that on logout if you want to clear the token you can just set

localStorage.setItem("token", "");

but be aware you can only set local storage to strings, not booleans or null.

UPDATE: I see that people are still referencing this, and wanted to add a caveat. On IOS, the local cache seems to be cleared by the OS automatically. I am not sure what triggers this, but I have ran into issues with and users loosing their settings stored in local storage. If that would be an issue for you, I recommend looking at something like IndexedDB, pouchdb, sqllite, or other alternatives.

like image 147
Jess Patton Avatar answered Nov 11 '22 21:11

Jess Patton