py-learn / src /app /auth /root-redirect.guard.ts
Oviya
Deploy Angular to HF Space
e4ab6d0
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
import { map, catchError, of } from 'rxjs';
export const rootRedirectGuard: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) => {
const authService = inject(AuthService);
const router = inject(Router);
return authService.checkSession().pipe(
map(() => {
if (authService.isLoggedIn()) {
router.navigate(['/home']);
} else {
router.navigate(['/auth']);
}
return false;
}),
catchError(() => {
router.navigate(['/auth']);
return of(false);
})
);
};