File size: 1,276 Bytes
6ecce98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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';

/**
 * Route guard to protect authenticated routes
 * Redirects unauthenticated users to login page with return URL
 */
export const authGuard: CanActivateFn = (
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
) => {
  const authService = inject(AuthenticationService);
  const router = inject(Router);

  // Check if user is already logged in locally
  if (authService.isLoggedIn()) {
    return true;
  }

  // Check session with server
  return authService.checkSession().pipe(
    map((isAuthenticated: boolean) => {
      if (isAuthenticated) {
        return true;
      }
      
      // Store intended destination for redirect after login
      localStorage.setItem('redirectAfterLogin', state.url);
      router.navigate(['/login']);
      return false;
    }),
    catchError(() => {
      // On error, redirect to login
      localStorage.setItem('redirectAfterLogin', state.url);
      router.navigate(['/login']);
      return of(false);
    })
  );
};