import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, RouterStateSnapshot, CanActivate, CanLoad, Route, UrlSegment, CanActivateChild, } from '@angular/router'; import { Observable } from 'rxjs'; import { tap, take } from 'rxjs/operators'; import { AuthService } from './auth.service'; @Injectable({ providedIn: 'root', }) export class AuthGuard implements CanActivate, CanLoad, CanActivateChild { constructor(private auth: AuthService) {} canLoad(route: Route, segments: UrlSegment[]): Observable { return this.auth.isAuthenticated$.pipe(take(1)); } canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable { return this.redirectIfUnauthenticated(state); } canActivateChild( childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable { return this.redirectIfUnauthenticated(state); } private redirectIfUnauthenticated( state: RouterStateSnapshot ): Observable { return this.auth.isAuthenticated$.pipe( tap((loggedIn) => { if (!loggedIn) { this.auth.loginWithRedirect({ appState: { target: state.url }, }); } }) ); } }