0

Upon refresh (i.e. F5), everything on Angular scope is cleared and reloaded, so how can I restore the page where the user left off, including keeping him logged in/authenticated? There's a couple suggestions about how I can accomplish this, but most of the helpful resources I've found are several years old, so the purpose of this question is to get a more updated answer and see what more experienced Angular developers have done and feel is the proper way to do this.

Here's one suggesting a service for sessionStorage >two years ago. Here's a blog post using a service for ngCookies from three years ago.

Ultimately, I'd like to also persist the authentication (probably using JSON Web Token) so that the user stays logged in until their token expires, in case that affects the decision between sessionStorage / ngCookies / or any more modern solutions.

Community
  • 1
  • 1
PDN
  • 771
  • 2
  • 13
  • 29

1 Answers1

0

The best way to retain the data on page refresh would be by fetching JSON data from an API and assigning the response to a scope variable. In this way, you need not store anything manually on localStorage/sessionStorage.

For authentication you can use auth(). Here is the link with angular : https://auth0.com/docs/client-platforms/angularjs

Nikhilesh K V
  • 1,480
  • 13
  • 20
  • What would you use to accomplish fetching JSON data? I'm thinking I could use routeResolve, or maybe something like ui-router/stateprovider? – PDN Apr 27 '16 at 21:14
  • Is your application entirely client side ? Is there no back end ? I was talking about fetching the JSON data from a Back end API. – Nikhilesh K V Apr 28 '16 at 06:21