I created lazy routes: one for the app shell module the other for auth module.
When I click on login then I redirect to shell app it's okay.
But when I click on logout and redirect to the main route (in order to trigger the guard) it's seems that guard don't invoke.
How do I know it? because the I don't see the console output after I click on logout.
it not even trigger the guard which is the problem. so I not redirect to login page.
Yes of course I can navigate directly to login route but I want to guard to do it for me.
So I create a simple app that demonstrate the problem in here and in codesandbox.io
import { BrowserModule } from "@angular/platform-browser";
import { Injectable, NgModule } from "@angular/core";
import { Component } from "@angular/core";
import {
ActivatedRouteSnapshot,
CanActivate,
CanActivateChild,
CanLoad,
Router,
RouterModule,
RouterStateSnapshot,
UrlTree
} from "@angular/router";
import { CommonModule } from "@angular/common";
import { Observable, of } from "rxjs";
let loggedIn = false; // <--- this is demo. it's set if the user login or not.
@Component({
template: '<button (click)="login()">login</button>'
})
export class LoginComponent {
constructor(private router: Router) {}
login() {
console.log("in login");
loggedIn = true;
this.router.navigate(["/"]);
}
}
@NgModule({
imports: [
CommonModule,
RouterModule.forChild([{ path: "", component: LoginComponent }])
],
declarations: [LoginComponent]
})
export class AuthModule {}
///////////////////////////////
@Injectable()
export class IsLoggedInGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(private readonly router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> {
return this.isAuthenticated(state.url);
}
canActivateChild(): Observable<boolean | UrlTree> {
return this.isAuthenticated();
}
canLoad(): Observable<boolean | UrlTree> {
return this.isAuthenticated();
}
private isAuthenticated(url?: string): Observable<boolean | UrlTree> | any {
if (!loggedIn) {
return this.router.createUrlTree(["/auth"], {
queryParams: { url }
});
}
return true;
}
}
////
@Component({
template: '<button (click)="logout()">logout</button>'
})
export class ShellComponent {
constructor(private router: Router) {}
logout() {
console.log("in logout");
loggedIn = false;
this.router.navigate(["/"]);
}
}
@NgModule({
imports: [
CommonModule,
RouterModule.forChild([{ path: "", component: ShellComponent }])
],
declarations: [ShellComponent]
})
export class ShellModule {}
@Component({
selector: "app-root",
template: `<router-outlet></router-outlet>`
})
export class AppComponent {
title = "CodeSandbox";
}
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
RouterModule.forRoot([
{
path: "",
loadChildren: () => Promise.resolve(ShellModule),
canActivate: [IsLoggedInGuard]
},
{ path: "auth", loadChildren: () => Promise.resolve(AuthModule) }
])
],
providers: [IsLoggedInGuard],
bootstrap: [AppComponent]
})
export class AppModule {}