Issue
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.
Solution
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.
Answered By - Jess Patton
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.