File size: 802 Bytes
e4ab6d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { inject } from '@angular/core';
import { CanActivateFn, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthenticationService } from './authentication.service';

//export const authenticationGuard: CanActivateFn = (route, state) => {
//  return true;
//};

export const authenticationGuard: CanActivateFn = (
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
) => {
  const authService = inject(AuthenticationService);
  const router = inject(Router);

  if (authService.isLoggedIn()) {
    return true;  // Allow access to the requested route
  } else {
    // Optionally, you can save the intended URL for redirection after login
    router.navigate(['/authentication']);
    return false;  // Deny access and redirect to login page
  }
};