Best way to store session information

10,770 views
Skip to first unread message

Ephraim Mower

unread,
Aug 6, 2012, 4:44:47 PM8/6/12
to ang...@googlegroups.com
Hi all!

I am new to Angular, and I have a scenario that I have not been able to fully figure out.

I have an application where I use an authentication service via xhr, and I am provided with a session id in a cookie to be used in future requests. So far I have been able to set up a form and log in successfully, but I'm missing some pieces:

1) I had to get rid of 'X-Requested-With': 'XMLHttpRequest' in the default headers setup in $HttpProvider in angular.js to get my auth request to work. I'm not sure if there is a better way of cross-domain requests to work. I did this based upon the message here: https://groups.google.com/forum/#!topic/angular/kvolRc4-9_0

2) I'm not sure how I might be able to access cookies in the success method of $http.post. I looked at the returned data, status, headers and config and did not see anything.

3) Once I am able to get the session id out of the cookies, what is the best way to store it for future requests?

Thanks in advance for any help you may be able to offer.

Regards,
Cork

Ephraim Mower

unread,
Aug 6, 2012, 4:49:04 PM8/6/12
to ang...@googlegroups.com
Whoops...never mind point #2. I'm trying to get to cookies from another domain like a big dummy :/

Michael Bielski

unread,
Aug 6, 2012, 4:49:40 PM8/6/12
to ang...@googlegroups.com
I had a similar situation and ended up storing the value in a cookie, and used some generic JS cookie code to make it happen. With cookies your session info is available to any page where you can get a cookie, including partials. The cookie functions I used are:

//Cookie functions

function createCookie(name, value) {
//All cookies are session cookies that expire as soon as the browser is closed.
    document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
//Since all cookies are session cookies, we only need to remove the data.
    createCookie(name, "");
}

You can set up a date/time stamp if you want them to persist longer than the immediate browser session.

Ephraim Mower

unread,
Aug 6, 2012, 9:46:12 PM8/6/12
to ang...@googlegroups.com
Thanks, Michael.

I've actually been able to see that the cookies are already being persisted in $cookies and get sent with subsequent requests, so I think I'm good. For some reason, though, the REST API I am consuming doesn't care about them - which is not an Angular problem :/

-Cork

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Michael Bielski

unread,
Aug 7, 2012, 11:19:56 AM8/7/12
to ang...@googlegroups.com
Would you mind sharing how you've been getting $cookies to work? I've not had any luck with that one yet.

Ephraim Mower

unread,
Aug 7, 2012, 11:28:09 AM8/7/12
to ang...@googlegroups.com
Once my REST API and my app were in the same domain, it "just worked". After authenticating to my service, $cookies had the cookie value, and it was used in all subsequent requests.

I included <script src="lib/angular/angular-cookies.js"></script> in my page, included ngCookies in my module declaration like so:

angular.module('app', ['app.filters', 'app.services', 'app.directives', 'ngCookies'])....

and injected $cookies into my controllers like so:

function MainCtrl($scope,$cookies)

On Tue, Aug 7, 2012 at 11:19 AM, Michael Bielski <michael...@yahoo.com> wrote:
Would you mind sharing how you've been getting $cookies to work? I've not had any luck with that one yet.

--

Michael Bielski

unread,
Aug 7, 2012, 12:08:51 PM8/7/12
to ang...@googlegroups.com
I've done all of that. It is actually using $cookies where I am having problems. It doesn't help that the docs are totally blank on this topic. I just don't see where to start.

Ephraim Mower

unread,
Aug 7, 2012, 12:13:47 PM8/7/12
to ang...@googlegroups.com
You should be able to get $cookies values but "dotting" to them, like $cookies.sessionid. I don't remember how I figured this out, but I don't think it was the docs :/

On Tue, Aug 7, 2012 at 12:08 PM, Michael Bielski <michael...@yahoo.com> wrote:
I've done all of that. It is actually using $cookies where I am having problems. It doesn't help that the docs are totally blank on this topic. I just don't see where to start.

--

Michael Bielski

unread,
Aug 7, 2012, 12:48:44 PM8/7/12
to ang...@googlegroups.com
Huh... never thought of that! Thanks!

Yann Driutti

unread,
Feb 14, 2013, 3:30:26 AM2/14/13
to ang...@googlegroups.com
Hi,
What about saving data in the sessionStorage ?
Or if we want to keep the data even if the browser is closed : the localStorage (with a timestamp if we want a validity period) ?
 
 
 
Yann

Michael Bielski

unread,
Feb 14, 2013, 10:42:55 AM2/14/13
to ang...@googlegroups.com
I actually recently converted my application to use localStorage/sessionStorage and I must say I am very happy with the results.

Paul Brickell

unread,
May 10, 2013, 11:48:09 AM5/10/13
to ang...@googlegroups.com
Example?

Grant Rettke

unread,
May 10, 2013, 11:59:45 AM5/10/13
to ang...@googlegroups.com
https://github.com/grevory/angular-local-storage
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Grant Rettke | ACM, AMA, COG, IEEE
gre...@acm.org | http://www.wisdomandwonder.com/
“Wisdom begins in wonder.” --Socrates
((λ (x) (x x)) (λ (x) (x x)))
“Life has become immeasurably better since I have been forced to stop
taking it seriously.” --Thompson

Luis De Avila

unread,
Jun 13, 2013, 9:30:14 PM6/13/13
to ang...@googlegroups.com
I'm having cookie issues as well. I'm a newbie to web development so pardon the ignorance. Could my issue be that my Angular app is running on localhost:4500 and my api server, that is sending the responses with cookies, is running on localhost:4000?

Thanks for the help!

Luis De Avila

unread,
Jun 13, 2013, 10:07:26 PM6/13/13
to ang...@googlegroups.com
Solved the issue. The issue was indeed that you cannot access cookies across domains. And running two different servers on two different ports on the same host is considered two different domains. Live and learn.
Reply all
Reply to author
Forward
0 new messages