Re: $cookie and $cookieStore - storing authentication data

13,906 views
Skip to first unread message

levyuk

unread,
Aug 28, 2012, 11:26:53 AM8/28/12
to ang...@googlegroups.com
Do you have any code example for using these?

I can store information using $cookieStore but it doesn't persist. When I close the browser and go to the page again there isn't any information stored.

This is my simple example for storing and retrieving from the cookieStore

            $scope.cookieTitles;

            $scope.getCookie = function () {
                $scope.cookieTitles = $cookieStore.titles;
            }

            $scope.setCookie = function () {
                $cookieStore.titles = $scope.titles;
            }

As I said, when the browser is closed and reopened the $cookieStore.titles is empty.

It would be great to know how to persist information.

Thanks
J

On Tuesday, 24 July 2012 05:36:38 UTC+1, Johan wrote:
I think the description of each in the documentation makes it quite clear:

$cookie just gives you access to cookies same as if you did by some other method.

$cookieStore uses cookies to make a key/value storage solution for you. 

So if you are starting something new and want to persist key/value pairs use $cookieStore

If you want to use existing cookie solution - say read/write cookies from your existing server session system then use $cookie.


On Saturday, July 21, 2012 11:35:22 AM UTC+12, Stephen Pitchford wrote:
Hi guys,

Can anyone explain with examples the difference between $cookies and $cookieStore. From looking at the source for cookies I can see that $cookieStore uses $cookies. Why use one over the other? And in fact, can $cookies be used by itself.

I am trying to work out how to manage cookies so that I can store authentication information about users in a cookie so when they try log in I can auto-login the user if their stored cookie credentials validate on the server. But since I can't seem to set the expiration time of the cookies I am not sure how to do this.

Thanks for the help,
Steve

levyuk

unread,
Aug 28, 2012, 12:07:57 PM8/28/12
to ang...@googlegroups.com
Ok so it looks like this is how it's supposed to be done 

$cookieStore.put('titles', $scope.titles);

Johan

unread,
Aug 28, 2012, 3:23:53 PM8/28/12
to ang...@googlegroups.com
Depending on what you want to store and which browsers you need to support you may be better off using localStorage or a solution like LawnChair. Cookies are limited to a few KB and also keep in mind that cookie data is sent with every request to your server.

levyuk

unread,
Aug 28, 2012, 3:27:53 PM8/28/12
to ang...@googlegroups.com
Hi Johan,

I only really want to store auth data so the size shouldn't be an issue. I think I've got it figured now which is a relief! 

I do like the idea of localstorage. Think it will be really handy in future. 

Cheers
J

Ephraim Mower

unread,
Aug 29, 2012, 9:55:10 AM8/29/12
to ang...@googlegroups.com
This is similar to something that I had to worry about recently too. This is a solution that uses Lawnchair and Promises:  https://gist.github.com/3456249 

danie...@gmail.com

unread,
Sep 23, 2012, 3:56:57 PM9/23/12
to ang...@googlegroups.com
I recently read an article, quote:

Anything stored in the browser is just one cross-site scripting attack away from being stolen. And there’s nothing to stop a virus or malware from pulling the Local Storage files off a compromised system. This means Local Storage is a great place for caching content or saving non-sensitive user data. But it’s a terrible place for storing passwords, credit card numbers or anything else that should be safely encrypted or tucked away under the security of a server-side data store.

Almog

unread,
Sep 23, 2012, 6:11:21 PM9/23/12
to ang...@googlegroups.com
Not sure I can fully help with a cookie example but just finished creating user login / authentication for our app the following really helped me  

Hope that helps 

exec...@gmail.com

unread,
Jan 7, 2013, 3:49:15 PM1/7/13
to ang...@googlegroups.com
I've noticed you can do $cookies['string key'] and it returns a string of the value for that key :D 

Not sure why the heck that's not in the documentation... seemed like a basic feature to have.

Peter Hedberg

unread,
Apr 8, 2014, 6:07:58 AM4/8/14
to ang...@googlegroups.com, exec...@gmail.com
It has nothing to do with Angular really, it's plain JavaScript.

obj.foo is the same as obj["foo"]
Reply all
Reply to author
Forward
0 new messages