I have the following Angular AuthGuard:
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanLoad {
constructor(private authService: AuthService, private router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.isAuthenticated();
}
canLoad(
route: Route,
segments: UrlSegment[],
): | boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
return this.isAuthenticated();
}
isAuthenticated(): | boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
return this.authService.isAuthenticated$.pipe(
take(1),
tap((isAuthenticated: boolean) => {
if (!isAuthenticated) {
this.router.navigate(['/account/login']);
}
}),
);
}
}
I updated to Angular 15:
Angular CLI: 15.2.0
Node: 16.19.1
Package Manager: npm 8.19.3
OS: darwin x64
Angular: 15.2.0
Package Version
---------------------------------------------------------
@angular-devkit/core 15.2.0
@angular-devkit/schematics 15.2.0
@schematics/angular 15.2.0
And now I'm getting the following TS depreciation note: And a similar depreciation message for the CanLoad interface:
@deprecated — Use CanMatchFn instead
'CanLoad' is deprecated.
I tried to look for how to implement my AuthGuard with the recommended CanActivateFn
and CanMatchFn
, but I couldn't find a good resource on you to implement this with the ability to redirect to a certain route when the user is not authenticated, as it appears in the old CanActivate and CanLoad implementation above:
this.router.navigate(['/account/login']);
How to correctly implement a simple Angular AuthGuard that uses the new CanMatchFn
and CanActivateFn
?