Commit 9d3f66f3 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

서버 authenticate 추가

parent c016fafd
...@@ -6,11 +6,37 @@ import isEmail from "validator/lib/isEmail"; ...@@ -6,11 +6,37 @@ import isEmail from "validator/lib/isEmail";
import { asyncWrap } from "../helpers"; import { asyncWrap } from "../helpers";
import { roleDb, userDb } from "../db"; import { roleDb, userDb } from "../db";
import { jwtCofig, envConfig, cookieConfig } from "../config"; import { jwtCofig, envConfig, cookieConfig } from "../config";
import { TypedRequest } from "../types";
export interface TypedRequestAuth<T> extends Request { export interface TypedRequestAuth<T> extends Request {
auth: T; auth: T;
} }
/**
* 함수를 호출하기 전에 req에 user 정보를 지정해야 합니다.
*/
export const authenticate = asyncWrap(
async (reqExp: Request, res: Response, next: NextFunction) => {
try {
const req = reqExp as TypedRequest;
if (req.auth) {
const { userId } = req.auth;
const user = req.user;
if (user && user.id === userId) {
return next();
} else {
throw new Error("권한이 필요합니다");
}
} else {
throw new Error("로그인이 필요합니다");
}
} catch (error: any) {
console.log(error);
return res.status(401).send(error.message || "권한 없음");
}
}
);
/** /**
* 지정된 역할 이상으로 권한이 있는지를 판단하는 미들웨어를 반환합니다. * 지정된 역할 이상으로 권한이 있는지를 판단하는 미들웨어를 반환합니다.
* @param roleName 역할 문자열 * @param roleName 역할 문자열
......
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