Session persistence with Angular (saving user's session_id between page refreshes)

So I have my Angular app setup with the angular-dreamfactory. I also have a UserService, implemented similarly to the way it is in the example trivia app. Everything works fine, but every time the page is reloaded, or browser window closed, the current logged in session is lost, and the user needs to login again to create a new session.

What is the recommended way to do session persistence? I could probably save the session_id in a cookie, but I’d then need to load it and use it, and deal with session expiration. Not exactly trivial.

I’d like to see how some of you guys implemented this.

I store the season_id in localStorage, not a cookie, and then check for that when the app bootstraps.
If the user never logs out (where you delete that session_id from local storage), a 401 will be returned when it tries to use that expired session_id, which pops up my login form.

Allright thanks, but if I store the session_id in a localStorage or cookie, and retrieve it during bootstrap, how do I tell my angular-dreamfactory SDK to use that session_id when making API requests?

Just set a $scope variable called session_id of your own. And link the header call for X-DreamFactorySession to it.
When you bootstrap the app, set that $scope variable to the value in localStorage.

Sorry it took so long to reply :confused:

1 Like