Tip #49[target URL in guards]

Suppose you develop an Angular application with certain pages with access restricted to logged in users:

In order to improve user experience, you may want to redirect to the target URL once a user has logged in:

When it comes to the canActivate guard, obtaining the target URL within the Auth guard is dead simple:

However, accessing the target URL within the canLoad guard is not so obvious. In order to accomplish the goal you need to call the getCurrentNavigation method and access the extractedUrl property:

The above solution works for both the canActivate and canLoad guards which simplifies the code.

Live example:

If you like the tip, please give me some applause 👏

--

--