Commit 04c66909 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

settup middleware and fix middleware

parent da199b03
import { Request, Response } from "express";
import { JwtCheck } from "@/components/lib/jwttoken";
/**
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
export const JwtAdminMiddleware = (req, res, next) => {
try {
const { token } = req.headers;
if (!token) throw 403;
let verify = JwtCheck(token);
console.log(verify.data);
if (!verify.data.rank) throw 403;
next();
} catch (err) {
return res.status(403).json({
status: 403,
message: "access denined",
});
}
};
...@@ -2,12 +2,12 @@ import express from "express"; ...@@ -2,12 +2,12 @@ import express from "express";
import WelcomeController from "../controllers/WelcomeController"; import WelcomeController from "../controllers/WelcomeController";
import UserController from "../controllers/UserController"; import UserController from "../controllers/UserController";
import adminRouter from "./admin"; import adminRouter from "./admin";
import { JwtAdminMiddleware } from "@/components/lib/jwttoken"; import { JwtAdminMiddleware } from "./middleware";
const route = express.Router(); const route = express.Router();
route.use('/admin/',JwtAdminMiddleware, adminRouter); route.use("/admin/", JwtAdminMiddleware, adminRouter);
route.get("/", WelcomeController.index); route.get("/", WelcomeController.index);
route.get('/user', UserController.index); route.get("/user", UserController.index);
route.post("/user/auth", UserController.login); route.post("/user/auth", UserController.login);
route.post("/user", UserController.create); route.post("/user", UserController.create);
......
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import { Request, Response, NextFunction } from "express";
/** /**
* *
* @param {{id: number, name: string, email: string, phone: string, photo: string, username: string }} data * @param {{id: number, name: string, email: string, phone: string, photo: string, username: string }} data
...@@ -15,23 +14,3 @@ export const JwtCheck = (token) => { ...@@ -15,23 +14,3 @@ export const JwtCheck = (token) => {
return { status: true, data: decoded }; return { status: true, data: decoded };
}); });
}; };
/**
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
export const JwtAdminMiddleware = (req, res, next) => {
try {
const { token } = req.headers;
if (!token) throw 403;
let verify = JwtCheck(token);
if (!verify.status) throw 403;
next();
} catch (err) {
return res.status(403).json({
status: 403,
message: "access denined",
});
}
};
...@@ -11,6 +11,7 @@ export default function Member() { ...@@ -11,6 +11,7 @@ export default function Member() {
let response = await axios.get("/api/admin/user", { let response = await axios.get("/api/admin/user", {
headers: { token: user.value.token }, headers: { token: user.value.token },
}); });
console.log(response.data);
setUserAll(response.data); setUserAll(response.data);
} catch (err) {} } catch (err) {}
} }
...@@ -19,9 +20,11 @@ export default function Member() { ...@@ -19,9 +20,11 @@ export default function Member() {
}, []); }, []);
return ( return (
<div> <div>
{userAll.map((usr, idx) => ( {userAll.map((usr, idx) => (
<div>{usr.name}@{usr.username} {usr.email}</div> <div key={idx}>
))} {usr.name}@{usr.username} {usr.email}
</div>
))}
<button onClick={fetchApi}>submit</button> <button onClick={fetchApi}>submit</button>
</div> </div>
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment