9

In AngularJS, I have a login controller that is redirected to by every single page in the app if the user arrives at the page and is not logged in. After the login sequence, I would like to redirect the user back to the page that they came from. What's the best way to do this? Save the old location in the $rootScope? Redirect to '/login?returnto=' + $location.path()? Is there a built in function?

Max
  • 8,671
  • 4
  • 33
  • 46

1 Answers1

5

You can use $rootScope or define a service to save the old location. Here is a SO post that contrasts the two approaches. I personally don't like adding returnto= to the URL.

This blog post, Authentication in AngularJS, might be of interest to you. Note the comment that Vojta (one of AngularJS authors) made on that blog post about using $rootScope -- he recommends a service instead.

Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
  • 1
    What if the login page is not a page handled by angular – justGoscha Jul 16 '14 at 16:44
  • 1
    @JustGoscha, use a cookie or a server-side session object to store the original page. After a successful login, check for the existance of the cookie or extract the page from the session and send it to to the browser (e.g., as some JavaScript or hidden form element). – Mark Rajcok Jul 17 '14 at 01:58