Storing user data and browser refresh

2,588 views
Skip to first unread message

chen....@gmail.com

unread,
Nov 15, 2012, 7:58:32 AM11/15/12
to ang...@googlegroups.com
Hi all,

In my application, I login to an api using username/password and I get an access_token from the server and some user data such as user name.
I store that information in a service (user service) or I use $rootScope.current_user to hold user data (I'm trying both ways).

All seems to work great until I refresh the browser (F5) - all service data or $rootScope data is gone. Everything reinitialized.
I imagine this is by design because no information is persisted.

How should I store the user info to be used around the application and not go away on a browser refresh? I don't want to use cookies because it seems wrong to save user information such as name, some stats and other information I get from the server.

Thanks,
-- Chen




Ryan Zec

unread,
Nov 15, 2012, 8:06:01 AM11/15/12
to ang...@googlegroups.com
The way I handle this is that there is a session back on the server that also stores this information.  When the browser is refreshed (or initially loads the application), I make a REST API request which checks to see if there is an active session on the server.  If there is, that request will return all the data just like a normal login would.  If there is no session, it then redirects the user to the login page.  I do it this way because I render the index.html page on the client side.

If you want to avoid the REST API call in this example, you can effectively do the same thing if you render the index.html on the server side.





--
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.
 
 

Chen Fisher

unread,
Nov 15, 2012, 10:02:47 AM11/15/12
to ang...@googlegroups.com
Thanks.

I'm actually doing something similar, but I thought there is a better option so I would avoid the extra api call.
What I do is store the access token I get from the server after a successful login in a cookie, and once the application is refreshed I use this token to query the api for the user information again.

What I don't like here is the call for the user information because I already got it on the user login. Just because a browser refresh I need to query it again...

Thanks again.

-- Chen

swa...@gmail.com

unread,
Aug 14, 2013, 3:39:59 PM8/14/13
to ang...@googlegroups.com, chen....@gmail.com
I am running into this same issue but it is causing me more problems.  We are using the $rootScope.$on("$routeChangeStart" event to check the users authentication level and determine if they can go to the page they are trying to access.  Even though I can reload all the user info this check gets hit before the REST API call returns the data.  Have either of you had to deal with that?
Reply all
Reply to author
Forward
0 new messages