auth guard

This commit is contained in:
eneller
2026-03-08 19:27:18 +01:00
parent cd568f0a63
commit c9a2cd8d66
6 changed files with 53 additions and 5 deletions

View File

@@ -0,0 +1,20 @@
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { APIService } from './api';
import { map } from 'rxjs/operators';
export const authGuard: CanActivateFn = (route, state) => {
const api = inject(APIService);
const router = inject(Router);
return api.isAuthenticated$.pipe(
map((isAuthenticated) => {
if (isAuthenticated) {
return true;
} else {
router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
})
);
};