Here's my scenario:
A user visits a route called /protected
. In my CanActivate guard, I check whether the user is logged in. If they are not logged in, I want to render my 404
route, but still show /protected
in the address bar. I want users to be unable to distinguish a protected page from a non-existent page.
This is what I tried and does not work.
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private router: Router, private userService: UserService) {}
canActivate() {
if (this.userService.getUser()) return true;
this.router.navigate(['/404']);
return false;
}
}
After routing, the url in the address bar will show /404
, but I want it to show /protected
.
Changing to this.router.navigate(['/404'], { skipLocationChange: true });
also does not work because the url in the address bar will be the previous url, not /protected
.
My question is: how do you render a different component if the user is not logged in while still keeping the url that they were trying to visit in the address bar?