| import { inject } from '@angular/core'; |
| import { CanActivateFn, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; |
| import { AuthenticationService } from '../services/authentication.service'; |
| import { map, catchError } from 'rxjs/operators'; |
| import { of } from 'rxjs'; |
|
|
| |
| |
| |
| |
| export const authGuard: CanActivateFn = ( |
| route: ActivatedRouteSnapshot, |
| state: RouterStateSnapshot |
| ) => { |
| const authService = inject(AuthenticationService); |
| const router = inject(Router); |
|
|
| |
| if (authService.isLoggedIn()) { |
| return true; |
| } |
|
|
| |
| return authService.checkSession().pipe( |
| map((isAuthenticated: boolean) => { |
| if (isAuthenticated) { |
| return true; |
| } |
| |
| |
| localStorage.setItem('redirectAfterLogin', state.url); |
| router.navigate(['/login']); |
| return false; |
| }), |
| catchError(() => { |
| |
| localStorage.setItem('redirectAfterLogin', state.url); |
| router.navigate(['/login']); |
| return of(false); |
| }) |
| ); |
| }; |
|
|