28 lines
725 B
TypeScript
28 lines
725 B
TypeScript
|
|
import { inject } from '@angular/core';
|
||
|
|
import { Router, type CanActivateFn } from '@angular/router';
|
||
|
|
import { AuthService } from '../services/auth.service';
|
||
|
|
|
||
|
|
export const adminGuard: CanActivateFn = async () => {
|
||
|
|
const authService = inject(AuthService);
|
||
|
|
const router = inject(Router);
|
||
|
|
|
||
|
|
// First check if authenticated
|
||
|
|
if (!authService.isAuthenticated()) {
|
||
|
|
// Try to refresh the token
|
||
|
|
const refreshed = await authService.refreshAccessToken();
|
||
|
|
if (!refreshed) {
|
||
|
|
router.navigate(['/login']);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Then check if admin
|
||
|
|
if (!authService.isAdmin()) {
|
||
|
|
// Not an admin, redirect to dashboard
|
||
|
|
router.navigate(['/dashboard']);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
};
|