-
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.
- Loading branch information
Showing
8 changed files
with
126 additions
and
58 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
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,7 @@ | ||
export const PROTECT_MESSAGES = { | ||
ROLE_ADMIN: "You are admin", | ||
ROLE_CUSTOMER: "You are customer", | ||
ROLE_EMPLOYEE: "You are employee", | ||
UNAUTHORIZED: "Unauthorized", | ||
MISSING_ACCESS_TOKEN: "Missing access token", | ||
} as const; |
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,20 +1,47 @@ | ||
import { Router } from "express"; | ||
import { wrapAsync } from "~/utils/handler"; | ||
import { NextFunction, Request, Response, Router } from "express"; | ||
import { accessTokenValidator } from "../user/user.middlewares"; | ||
const routes: Module[] = require("./mapRouteWithRole.json"); | ||
|
||
const protectRouter = Router(); | ||
|
||
protectRouter.post("/protect", (req, res) => { | ||
let msg = "You are not authorized"; | ||
const { role } = req.body; | ||
if (role === 0) { | ||
return res.status(200).send("You are admin"); | ||
} else if (role === 1) { | ||
return res.status(200).send("You are customer"); | ||
} else if (role === 2) { | ||
return res.status(200).send("You are employee"); | ||
} else { | ||
return res.status(401).json({ message: msg }); | ||
// 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 }; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
}); | ||
|
||
protectRouter.use((req: Request, res: Response, next: NextFunction) => { | ||
const url = req.path; | ||
|
||
console.log("size: " + openRoutes.length); | ||
|
||
openRoutes.forEach((route) => console.log(route)); | ||
|
||
console.log(openRoutes.includes(url)); | ||
|
||
if (openRoutes.includes(url)) { | ||
return next(); | ||
} | ||
accessTokenValidator(req, res, next); | ||
}); | ||
|
||
export default protectRouter; |
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
Oops, something went wrong.