2

I'm building an Angular app in which I'd like to redirect the user to the last visited page he was at after logging in. The idea would be that if you are logged in, I will fetch from a local storage the last visited page (which will get updated on each navigation) and it will redirect there. If you are not logged in then you go to the login page.

For this I have been trying to find the best way to accomplish this since I believe there are a couple of options.

1- Using an App initializer. Idea would be to define a function I can set to be executed when the app is being initialized to detect where to redirect to. Problem with this is that I'm getting an error since I can inject the Router at this point since it hasn't been created yet. An alternative is to use the Injector class to generate a Router instance and from there redirect. This seems rather dirty.

2- Using a Route Resolver. Despite this seems more as to prepare data that then will be showed, here I could define the functionality when navigating to the base path ''. This works fine at first, but if I hit a back button to a main dashboard page (suppose we were redirected to '/dash/msgs' when accessing the site, and then we want to go back to path '/dash/main') then the resolver is executed again, the stored navigation hasn't yet been updated (which happens on a service at navigation end event) so the old value is being retrieved and the state.url from RouterStateSnapshot shows me the main path '/' rather than the end path '/dash/main'. So I end up always on the same page - this is not the case when I access without being logged in and then I log in and start navigating with the same browser window

3- Using a Guard with a can activate. This also doesn't seem the right place to me. I defined a guard to my main dashboard module (for path '/dash') which will always be executed when accessing from a new browser window and the user is logged in. This would happen after a first guard to detect that you are indeed logged in, but this guard will return false if there was some navigation url on the local storage and redirect there which will be in most cases. This approach works just fine, but it has some delay and doesn't seem the right approach to me since I would expect to detect where to navigate on an earlier stage

Does someone could point me out the proper strategy to handle this? Thanks in advance!

Rodrigo Martinez
  • 913
  • 3
  • 13
  • 29
  • Why not create a default "Home Page" that the user is always routed to, then in the `onInit` method of that check local storage and see if you need to navigate? – Vlad274 Apr 03 '19 at 15:32
  • That could be an approach, but the thing is that doing it that way you are rendering the home component while you are redirected. It's too late. I was hoping for a solution that would resolve this earlier – Rodrigo Martinez Apr 03 '19 at 16:16
  • You always have to render some component before processing the router, IE some component contains the `router-outlet`. Maybe do this logic in that `onInit`? – Vlad274 Apr 03 '19 at 16:27

0 Answers0