py-learn / src /app /authentication /authentication.guard.ts
Oviya
Deploy Angular to HF Space
e4ab6d0
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
}
};