Added guards for auth and role
This commit is contained in:
+19
-8
@@ -1,4 +1,6 @@
|
|||||||
import {Routes} from '@angular/router';
|
import {Routes} from '@angular/router';
|
||||||
|
import {authGuard} from "./guards/auth.guard";
|
||||||
|
import {roleGuard} from "./guards/role.guard";
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{
|
{
|
||||||
@@ -22,35 +24,44 @@ export const routes: Routes = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'dashboard',
|
path: 'dashboard',
|
||||||
loadComponent: () => import('./pages/dashboard/dashboard').then(m => m.Dashboard)
|
loadComponent: () => import('./pages/dashboard/dashboard').then(m => m.Dashboard),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'stock',
|
path: 'stock',
|
||||||
loadComponent: () => import('./pages/stock/stock').then(m => m.Stock)
|
loadComponent: () => import('./pages/stock/stock').then(m => m.Stock),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'supplier',
|
path: 'supplier',
|
||||||
loadComponent: () => import('./pages/supplier/supplier').then(m => m.Supplier)
|
loadComponent: () => import('./pages/supplier/supplier').then(m => m.Supplier),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'deliverer',
|
path: 'deliverer',
|
||||||
loadComponent: () => import('./pages/deliverer/deliverer').then(m => m.Deliverer)
|
loadComponent: () => import('./pages/deliverer/deliverer').then(m => m.Deliverer),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'quotation',
|
path: 'quotation',
|
||||||
loadComponent: () => import('./pages/quotation/quotation').then(m => m.Quotation)
|
loadComponent: () => import('./pages/quotation/quotation').then(m => m.Quotation),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'purchase-order',
|
path: 'purchase-order',
|
||||||
loadComponent: () => import('./pages/purchase-order/purchase-order').then(m => m.PurchaseOrder)
|
loadComponent: () => import('./pages/purchase-order/purchase-order').then(m => m.PurchaseOrder),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'delivery-note',
|
path: 'delivery-note',
|
||||||
loadComponent: () => import('./pages/delivery-note/delivery-note').then(m => m.DeliveryNote)
|
loadComponent: () => import('./pages/delivery-note/delivery-note').then(m => m.DeliveryNote),
|
||||||
|
canActivate: [authGuard]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'user',
|
path: 'user',
|
||||||
loadComponent: () => import('./pages/user/user').then(m => m.User)
|
loadComponent: () => import('./pages/user/user').then(m => m.User),
|
||||||
|
canActivate: [authGuard, roleGuard],
|
||||||
|
data: {roles: ['Admin']},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '**',
|
path: '**',
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { inject } from '@angular/core';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
|
import { AuthService} from "../services/auth.service";
|
||||||
|
|
||||||
|
export const authGuard = () => {
|
||||||
|
const auth = inject(AuthService);
|
||||||
|
const router = inject(Router);
|
||||||
|
|
||||||
|
return auth.isLoggedIn() ? true : router.parseUrl('/login');
|
||||||
|
};
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { inject } from '@angular/core';
|
||||||
|
import { ActivatedRouteSnapshot, Router } from '@angular/router';
|
||||||
|
import { AuthService} from "../services/auth.service";
|
||||||
|
|
||||||
|
export const roleGuard = (route: ActivatedRouteSnapshot) => {
|
||||||
|
const auth = inject(AuthService);
|
||||||
|
const router = inject(Router);
|
||||||
|
|
||||||
|
const requiredRoles: string[] = route.data['roles'];
|
||||||
|
|
||||||
|
return auth.hasRole(requiredRoles) ? true : router.parseUrl('/dashboard');
|
||||||
|
};
|
||||||
@@ -34,4 +34,36 @@ export class AuthService {
|
|||||||
localStorage.removeItem('jwt');
|
localStorage.removeItem('jwt');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
decodeToken(): { sub?: string; role?: string; exp?: number } | null {
|
||||||
|
const token = this.getToken();
|
||||||
|
if (!token) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const payload = token.split('.')[1];
|
||||||
|
return JSON.parse(atob(payload));
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoggedIn(): boolean {
|
||||||
|
const token = this.getToken();
|
||||||
|
if (!token) return false;
|
||||||
|
|
||||||
|
const decoded = this.decodeToken();
|
||||||
|
if (!decoded?.exp) return true;
|
||||||
|
|
||||||
|
return decoded.exp * 1000 > Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
getRole(): string | null {
|
||||||
|
return this.decodeToken()?.role ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
hasRole(requiredRoles: string[]): boolean {
|
||||||
|
const role = this.getRole();
|
||||||
|
if (!role) return false;
|
||||||
|
return requiredRoles.includes(role);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user