20230802FertigstellungVonBookMonkey
This commit is contained in:
21
src/app/shared/auth.guard.ts
Normal file
21
src/app/shared/auth.guard.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
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');
|
||||
}
|
||||
}),
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user