AngularJS: redirecting to login page, but remember the starting location?

I managed to get a working security authentication, so basically I am redirecting to the login login, since the user is not logged in.

This works fine, but I was wondering if someone is doing something similar, as soon as I get redirected to the login page, I would like to return to the original page after they authenticated. Asp.net mvc does this with returnUrl in querystring, but it is really ugly, I think.

So, I have a.

/items // this show my items.. /login // this is the login view. 

therefore, if the user goes to / items and does not register, I am currently redirected to the login window, but after successful login, I need to redirect the user to where he wanted to execute the original go ie / items

I'm doing it now

  $rootScope.$on("$routeChangeStart", function ( next, current) { if (!authentication.getIsLoggedIn()) $location.path('/login'); }); 

If I can’t pass the original url or save it somehow, like after a successful login, I don’t know where to send the user.

Love to hear any feedback.

thanks

+4
source share
1 answer

Agree with @NuclearGhost, you are very close. I handle this by having an AppLevel controller that simply supports the next value and doing redirectTo () to get them back to where they were going after authentication.

+1
source

Source: https://habr.com/ru/post/1491131/


All Articles