21 lines
728 B
TypeScript
21 lines
728 B
TypeScript
import {inject} from "@angular/core";
|
|
import {CanActivateFn, Router} from "@angular/router";
|
|
import {AuthenticationService} from "../service/authentication.service";
|
|
import {MatSnackBar} from "@angular/material/snack-bar";
|
|
|
|
export const authenticationGuard: CanActivateFn = async () => {
|
|
const authenticationService = inject(AuthenticationService);
|
|
const router = inject(Router);
|
|
const snackBar = inject(MatSnackBar);
|
|
|
|
await authenticationService.checkIsAuthenticated();
|
|
|
|
if (authenticationService.isAuthenticated()) {
|
|
return true;
|
|
} else {
|
|
router.navigate(['/login']);
|
|
snackBar.open($localize`You are unauthenticated. Please, log-in first.`, $localize`Close`, {duration: 5000});
|
|
return false;
|
|
}
|
|
}
|