-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add type casting protect route, separate processing functio…
…n to each file fix: fix protect route conflict with role
- Loading branch information
Showing
7 changed files
with
116 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { UserRole } from "../user/user.enum"; | ||
import { RouteConfig } from "./protect.schemas"; | ||
|
||
export const routesConfig: RouteConfig[] = [ | ||
{ | ||
contextPath: "/admin", | ||
roles: [UserRole.Admin], | ||
}, //Admin can access all | ||
{ contextPath: "/user", roles: [UserRole.Customer, UserRole.Admin] }, // User only access to Customer | ||
{ contextPath: "/employee", roles: [UserRole.Employee, UserRole.Admin] }, // Employee only access to Employee | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { UserRole } from "../user/user.enum"; | ||
|
||
export interface Route { | ||
api: string; | ||
method: string; | ||
access_token: boolean; | ||
} | ||
|
||
// Interface for a Module object with nested routes | ||
export interface Module { | ||
module: string; | ||
route: Record<string, Route | Route[]>; | ||
} | ||
|
||
export interface RouteConfig { | ||
contextPath: string; | ||
roles: UserRole[]; | ||
} | ||
|
||
export type RequestPath = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,50 @@ | ||
import { UserRole } from "../user/user.enum"; | ||
const routes: Module[] = require("./mapRouteWithRole.json"); | ||
import rawRoutes from "./mapRouteWithRole.json"; | ||
import { routesConfig } from "./protect.configs"; | ||
import { Module, RouteConfig } from "./protect.schemas"; | ||
|
||
// Interface for a Route object within a module | ||
interface Route { | ||
api: string; | ||
method: string; | ||
role: number; | ||
access_token: boolean; | ||
} | ||
|
||
// Interface for a Module object with nested routes | ||
interface Module { | ||
module: string; | ||
route: { [key: string]: Route }; | ||
} | ||
|
||
interface RouteConfig { | ||
path: string; | ||
roles: UserRole[]; | ||
} | ||
|
||
export const routesConfig: RouteConfig[] = [ | ||
{ | ||
path: "/admin", | ||
roles: [UserRole.Employee, UserRole.Customer, UserRole.Admin], | ||
}, //Admin can access all | ||
{ path: "/user", roles: [UserRole.Customer] }, // User only access to Customer | ||
{ path: "/employee", roles: [UserRole.Employee] }, // Employee only access to Employee | ||
]; | ||
// Cast rawRoutes to unknown first, then to Module[] | ||
const routes: Module[] = rawRoutes as unknown as Module[]; | ||
|
||
export function getOpenRoutes(): string[] { | ||
const openRoutes: string[] = []; | ||
|
||
// Filter routes where access_token is false | ||
routes.forEach((module: Module) => { | ||
for (const key in module.route) { | ||
if (!module.route[key].access_token) { | ||
openRoutes.push(module.route[key].api); | ||
const route = module.route[key]; | ||
//if in case of multiple routes | ||
if (Array.isArray(route)) { | ||
route.forEach((r) => { | ||
if (!r.access_token) { | ||
openRoutes.push(r.api); | ||
} | ||
}); | ||
} else { | ||
if (!route.access_token) { | ||
openRoutes.push(route.api); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return openRoutes; | ||
} | ||
|
||
// math the route.path with the req.path (/user/login) but take the first part only (/user) | ||
// match the route.contextPath with the req.path (/user/login) but take the first part only (/user) | ||
// ex: /user/login -> /user : { contextPath: '/user', roles: [ 0,1 ]} | ||
// ex: /user/login -> /admin : undefined | ||
export function checkRole( | ||
path: string, | ||
pattern: string, | ||
requestPath: string, | ||
delimiter: string, | ||
): RouteConfig | undefined { | ||
return routesConfig.find( | ||
(route) => route.path === pattern + path.split(pattern)[1], | ||
(route) => | ||
route.contextPath === delimiter + requestPath.split(delimiter)[1], | ||
); | ||
} | ||
|
||
// Type guard to check if a number is a valid UserRole | ||
export function isValidUserRole(role: UserRole): boolean { | ||
return Object.values(UserRole).includes(role); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.