22 lines
592 B
TypeScript
22 lines
592 B
TypeScript
import { Inject, inject } from '@angular/core';
|
|
import { CanActivateFn, Router } from '@angular/router';
|
|
import { AuthService } from './auth.service';
|
|
import { map, take } from 'rxjs';
|
|
|
|
export const authGuard: CanActivateFn = (route, state) => {
|
|
const authService = inject(AuthService);
|
|
const router = inject(Router);
|
|
|
|
return authService.isAuthenticated$.pipe(
|
|
take(1),
|
|
map((isAuthenticated) => {
|
|
if (authService.isAuthenticated) {
|
|
return true;
|
|
} else {
|
|
window.alert('Not logged in!');
|
|
return router.parseUrl('home');
|
|
}
|
|
}),
|
|
);
|
|
};
|