# localStorage and logged-in user

**URL:** <https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048>\
**Category:** uncategorized\
**Created:** [March 20, 2014, 9:26am UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048 "2014-03-20T09:26:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piernik](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Piernik](https://forum.ionicframework.com/u/Piernik)\
**Post date:** [March 20, 2014, 9:26am UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/1 "2014-03-20T09:26:48Z")

</div>

In my App user can login using website’s account.  
After loggin-in I want to keep user id in localStorage. Is it safe? Can localStorage be manually changed on Android? If not how to storage data (like cookies in website)?

---

<div class="post-metadata">

**Author:** ![Scott](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@Scott](https://forum.ionicframework.com/u/Scott)\
**Post date:** [March 20, 2014, 9:45am UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/2 "2014-03-20T09:45:43Z")

</div>

You should assume any data on the users device is not safe so don’t just store the user id and assume it’s valid.

The most common approach to this problem is to generate a token that represents the user and store that. You would generate this token on the server and return it with the response to your login call.

It would also be best to not try and write this stuff yourself. You should be able to find a library for your server side language of choice to take care of it for you.

---

<div class="post-metadata">

**Author:** ![Piernik](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Piernik](https://forum.ionicframework.com/u/Piernik)\
**Post date:** [March 20, 2014, 9:51am UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/3 "2014-03-20T09:51:05Z")

</div>

Thanks - and what if I store user id as well as md5(password)?

---

<div class="post-metadata">

**Author:** ![Alias](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@Alias](https://forum.ionicframework.com/u/Alias)\
**Post date:** [March 20, 2014, 11:18am UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/4 "2014-03-20T11:18:30Z")

</div>

Don’t use MD5, it is considered broken now.

Here’s one way of going about this problem. As far as I know, it’s a safe method… There will be better ones though.

You have a database online, which for example has a username and hashed password (one that cannot be un-hashed to get the original password).

You have a server, which has some sort of API which accepts HTTP requests from somewhere, in this case your app. So for login:

```
http://api.com/login

```

The app user wants to login. They enter their username and password, and hit login. Before the request is sent, you hash that password on your app and send it off (using a GET request here):

```
http://api.com/login?username=bob&password=myhashedpasswordstring

```

Your server then checks for a match in the database. If there is no match, the response tells them they have failed. However, if there is a match we can log them in. So in the success response, we respond with the “user\_id” and a unique generated token which the server API has made, using the “user\_id”. Only the server knows how to create this token and what algorithm/hashing is made (using sha256, base64encode together etc), eg:

```
substr(base64_encode(hash_hmac('sha256', <user_id>, "MySuperSecretString")), 0, 32);

```

On your app you store the user\_id and token in local storage.

Now lets say you have a API call which requires a valid user/login - called “data”, and we have to specify how much data we want using the “number” parameter. We can send a request like so:

```
http://api.com/data?number=10

```

But this request also requires a new “auth” parameter, which in this case is formatted like: user\_id|token

Because the | (pipe) symbol needs to be url encoded, it will encoded as “%7C”, see: [http://www.w3schools.com/tags/ref\_urlencode.asp](http://www.w3schools.com/tags/ref_urlencode.asp)

Lets assume bobs user\_id = “2”, and his token = “ABCDEFG” So the request will now be:

```
http://api.com/data?number=10&auth=2%7CABCDEFG

```

When this hits your server, you first need to grab the “auth” parameter. This then needs decoding, so you’d end up with (PHP syntax for example):

```
$auth = "2|ABCDEFG";

```

Now you’d explode this string using the | symbol, and now you have your user\_id and given token.

To verify this is correct, you’d run the user\_id through the token generator algorithm. You’d then match the result of that, with the token that was given in your “auth” parameter.

If they don’t match up, you assume that someone has tampered with either the “user\_id” or “token” in the apps local storage, and thus reject the request and do whatever on the app (force logout, show an error etc).

If they do match up, then you can assume the user is genuine, and serve up the data.

Hope that helps… One way of doing it without OAuth etc.

EDIT: fixed a link

---

<div class="post-metadata">

**Author:** ![Scott](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@Scott](https://forum.ionicframework.com/u/Scott)\
**Post date:** [March 20, 2014, 1:47pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/5 "2014-03-20T13:47:29Z")

</div>

I might just say again, please don’t write this stuff yourself. Find a trusted library for your server language of choice. Security is hard!

Please also realise that any token based scheme should only be used over https otherwise you are sending your auth tokens in plain text over the wire.

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [March 20, 2014, 2:58pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/6 "2014-03-20T14:58:25Z")

</div>

Don’t put the token in the URL. You need to inject authorization headers into your requests. You need to look at AngularJS docs and tutorials about default headers and interceptors.

---

<div class="post-metadata">

**Author:** ![Robin](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robin/32/64162_2.png) [@Robin](https://forum.ionicframework.com/u/Robin)\
**Post date:** [March 20, 2014, 3:04pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/7 "2014-03-20T15:04:03Z")

</div>

@Calendee You should give him that article about security in AngularJS you posted earlier. I believe it was from Auth0?

Edit; Found it already. @Piernik read up on this:

[http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/](http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/)

---

<div class="post-metadata">

**Author:** ![Alias](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@Alias](https://forum.ionicframework.com/u/Alias)\
**Post date:** [March 20, 2014, 3:13pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/8 "2014-03-20T15:13:25Z")

</div>

Cheers handy article. Still learning myself, just one way I thought logical to do it 😄

---

<div class="post-metadata">

**Author:** ![JavierMartinz](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@JavierMartinz](https://forum.ionicframework.com/u/JavierMartinz)\
**Post date:** [March 20, 2014, 3:29pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/9 "2014-03-20T15:29:46Z")

</div>

> [@Robin](#):
>
> [http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/](http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/)

Is there any doc or tutorial about refresh token flow and AngularJS? I mean, the right use of http interceptors and headers to detect that token has expired and ask to API for a new one.

Thanks!

---

<div class="post-metadata">

**Author:** ![Piernik](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Piernik](https://forum.ionicframework.com/u/Piernik)\
**Post date:** [March 20, 2014, 3:51pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/10 "2014-03-20T15:51:27Z")

</div>

Thank every one for usefull hints.

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [March 20, 2014, 7:26pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/11 "2014-03-20T19:26:06Z")

</div>

Good discussion. I found this library extremely useful. I intend to write a blog post about using it with ionic.

> **[witoldsz/angular-http-auth](https://github.com/witoldsz/angular-http-auth)**
>
> Contribute to witoldsz/angular-http-auth development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Piernik](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Piernik](https://forum.ionicframework.com/u/Piernik)\
**Post date:** [March 20, 2014, 7:31pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/12 "2014-03-20T19:31:16Z")

</div>

If Your write it share it here

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [March 21, 2014, 3:02am UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/13 "2014-03-21T03:02:11Z")

</div>

I certainly will post it here. I am planning on writing it over the weekend.

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [March 24, 2014, 8:35pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/14 "2014-03-24T20:35:46Z")

</div>

Here is the article I wrote over the weekend and promised to post here:

[http://www.kdmooreconsulting.com/blogs/authentication-with-ionic-and-angular-js-in-a-cordovaphonegap-mobile-web-application/](http://www.kdmooreconsulting.com/blogs/authentication-with-ionic-and-angular-js-in-a-cordovaphonegap-mobile-web-application/)

---

<div class="post-metadata">

**Author:** ![programming\_kid](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@programming\_kid](https://forum.ionicframework.com/u/programming_kid)\
**Post date:** [May 4, 2014, 9:41pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/15 "2014-05-04T21:41:04Z")

</div>

can you recommend any libraries for node or php to solve this problem

---

<div class="post-metadata">

**Author:** ![victtavares](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@victtavares](https://forum.ionicframework.com/u/victtavares)\
**Post date:** [October 15, 2014, 8:35pm UTC](https://forum.ionicframework.com/t/localstorage-and-logged-in-user/2048/16 "2014-10-15T20:35:18Z")

</div>

In this example, what is preventing someone else to sniff the local storage and make the request as the user?

For example, I can have access to the user localStorage, get his token and make a request as him, grabbing all his data instead of mine.
