Skip to content Skip to sidebar Skip to footer

Browser Refresh - Lost Servicestack Authentication Session Data

I have an angular.js single page app that authenticates against a RESTful API (Servicestack). This all works fine. When the response from the authentication api is returned the u

Solution 1:

ServiceStack Authentication uses cookies to store the session token by default. Which means your Angular application will receive the cookie when you first sign in. It will pass this for subsequent requests, and they will succeed while the session is still valid on the server.

The problem will be that Angular will lose the object state, when you refresh the page, that is telling it you have an active session. So you must restore this knowledge to Angular. There are two ways to tackle this:

  1. Check for the ss-id cookie when you application starts and assume you have a valid session. In other words, so restore to a signed in state, until you get a 401 error from the server. This is the quickest approach, and doesn't require additional overhead to check the session if somebody refreshes the page.

  2. Check for the ss-id cookie and make a test authenticated request to check the session is still valid.

If you need to restore other information such as the current logged in user's name etc, then you would need to store that in a cookie/local storage to restore it on refresh, or go with method 2, and retrieve it back from the server.

You can use $cookies provider to manage the session cookie.

Post a Comment for "Browser Refresh - Lost Servicestack Authentication Session Data"