# How to stay logged in and preserve local storage after user closes app?

**URL:** <https://forum.ionicframework.com/t/how-to-stay-logged-in-and-preserve-local-storage-after-user-closes-app/14813>\
**Category:** uncategorized\
**Created:** [December 29, 2014, 3:12am UTC](https://forum.ionicframework.com/t/how-to-stay-logged-in-and-preserve-local-storage-after-user-closes-app/14813 "2014-12-29T03:12:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkerny45](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@mkerny45](https://forum.ionicframework.com/u/mkerny45)\
**Post date:** [December 29, 2014, 3:12am UTC](https://forum.ionicframework.com/t/how-to-stay-logged-in-and-preserve-local-storage-after-user-closes-app/14813/1 "2014-12-29T03:12:52Z")

</div>

I have implemented a HTTP auth sign up/login utilizing JSON Web Tokens ([https://github.com/sahat/satellizer](https://github.com/sahat/satellizer)) where the token is saved in local storage after authentication. However, I have discovered after I emulate and build the app on my iPhone, go ahead and login, and then close the app, the user’s login session is destroyed.

Is local storage preserved when a user closes the app? Have I made a possible oversight in my technical implementation? How can I go about keeping a user logged in once they’ve closed the app?

---

<div class="post-metadata">

**Author:** ![Nayan](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@Nayan](https://forum.ionicframework.com/u/Nayan)\
**Post date:** [December 29, 2014, 12:19pm UTC](https://forum.ionicframework.com/t/how-to-stay-logged-in-and-preserve-local-storage-after-user-closes-app/14813/2 "2014-12-29T12:19:56Z")

</div>

In these [link](http://forum.ionicframework.com/t/what-persistent-data-storages-should-i-use/3707/21) most options describes, which might help to you.

If you want to use local storage then used as follow.  
set variable in your login controller with local storage and if you want them used in entire application then defined such variable in factory file.

Set Local storage variable :

`localStorage.setItem("var_1",value1);`  
`localStorage.setItem("var_2",value2);` and so on.

Get variable value in app.js file and check whether the login details like username, password, auth token and other information available or not :

`localStorage.getItem("var_1");`  
`localStorage.getItem("var_2");` and so on.

---

<div class="post-metadata">

**Author:** ![rcbytes](https://avatars.discourse-cdn.com/v4/letter/r/e68b1a/32.png) [@rcbytes](https://forum.ionicframework.com/u/rcbytes)\
**Post date:** [December 29, 2014, 12:40pm UTC](https://forum.ionicframework.com/t/how-to-stay-logged-in-and-preserve-local-storage-after-user-closes-app/14813/3 "2014-12-29T12:40:13Z")

</div>

Maybe you’re using **sessionStorage** instead of **localStorage**. sessionStorage is cleared after app closes.  
[http://docs.phonegap.com/en/3.0.0/cordova\_storage\_storage.md.html#localStorage](http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#localStorage)

In both cases it is a good pratice wrap it in a service then future changes on Storage won’t impact so much 😄  
[https://gist.github.com/rcbytes/0f977e56c6e6da146b35](https://gist.github.com/rcbytes/0f977e56c6e6da146b35)

–Edit-- @jrschumacher

Or if you want something complete, my 2 cents:

> **[scotttrinh/angular-localForage](https://github.com/scotttrinh/angular-localForage)**
>
> angular-localForage - Angular service & directive for https://github.com/mozilla/localForage (Offline storage, improved.)

---

<div class="post-metadata">

**Author:** ![jrschumacher](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jrschumacher/32/65147_2.png) [@jrschumacher](https://forum.ionicframework.com/u/jrschumacher)\
**Post date:** [December 29, 2014, 1:45pm UTC](https://forum.ionicframework.com/t/how-to-stay-logged-in-and-preserve-local-storage-after-user-closes-app/14813/4 "2014-12-29T13:45:24Z")

</div>

There are a number of AngularJS wrappers around localStorage. My goto:

[http://ngmodules.org/modules/ngStorage](http://ngmodules.org/modules/ngStorage)
