26 lines
900 B
TypeScript
26 lines
900 B
TypeScript
import { inject } from '@angular/core';
|
|
import { CanActivateFn, Router } from '@angular/router';
|
|
|
|
import { ToasterService } from '../core/toaster.service';
|
|
|
|
import { AuthService } from './auth.service';
|
|
|
|
export const authGuard: CanActivateFn = (route, state) => {
|
|
const user = inject(AuthService).user;
|
|
const permission =
|
|
route.data['permission'] === undefined
|
|
? route.data['permission']
|
|
: route.data['permission'].replace(/ /g, '-').toLowerCase();
|
|
if (!user) {
|
|
// not logged in so redirect to login page with the return url
|
|
inject(Router).navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
|
return false;
|
|
}
|
|
if (permission !== undefined && user.perms.indexOf(permission) === -1) {
|
|
inject(ToasterService).show('Danger', 'You do not have the permission to access this area.');
|
|
return false;
|
|
}
|
|
// logged in so return true
|
|
return true;
|
|
};
|