I'm trying to load the home page of my app for visitors who are not authenticated.
const routes: Routes = [
{ path: '', loadChildren: './home/home.module#HomeModule' }
...
Authenticated users should get their feed via that module, also on the empty path.
{ path: '', loadChildren: './feed/feed.module#FeedModule', canActivate: [IsAuthenticationGuard] },
{ path: '', loadChildren: './home/home.module#HomeModule', canActivate: [NoAuthenticationGuard] },
I would expect that IsAuthenticationGuard
would fail and load the default home component.
Instead it DOES download the feed module package (shown in the network tab) but loads nothing in the router outlet. Very confusing.
How can I do conditional routing (based on guards or otherwise) on the empty path?
Update: Here are the guards by request
@Injectable()
export class IsAuthenticationGuard implements CanActivate {
constructor(
private authenticationService: AuthenticationService
) { }
public canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> {
return this.authenticationService.isAuthenticated.pipe(take(1), map((isAuthentication) => {
return isAuthentication;
}));
}
}
I've researched the new urlTree
and it's cool that you can now redirect via the route instead of within the guard. However, redirects don't seem applicable if you're trying to use the same route with a different module. Plz correct me if there is a way.