Commit 35f8698d authored by Lee Soobeom's avatar Lee Soobeom
Browse files

useNavigate useLocation

parent 0a4db016
...@@ -51,11 +51,15 @@ export default function Posting() { ...@@ -51,11 +51,15 @@ export default function Posting() {
formdata.append("picture", filelist?.[0]); formdata.append("picture", filelist?.[0]);
const res = await postApi.createFileAndPost(formdata); const res = await postApi.createFileAndPost(formdata);
return res;
} else { } else {
for (var i = 0; i < filelist.length; i++) { for (var i = 0; i < filelist.length; i++) {
formdata.append("picture", filelist?.[i]); formdata.append("picture", filelist?.[i]);
} }
const res = await postApi.createFileAndPost(formdata); const res = await postApi.createFileAndPost(formdata);
return res;
} }
} }
}; };
...@@ -69,10 +73,10 @@ export default function Posting() { ...@@ -69,10 +73,10 @@ export default function Posting() {
if (postingFormMatch(user, file)) { if (postingFormMatch(user, file)) {
setLoading(true); setLoading(true);
if (file) { if (file) {
const res = sendImg2Db(file); const postRes = await sendImg2Db(file);
// console.log(res); // console.log("Newpost Info", postRes);
navigate("/board", { replace: true, state: postRes });
} }
navigate("/board", { replace: true });
setSuccess(true); setSuccess(true);
setError(""); setError("");
} }
......
...@@ -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 역할 문자열
......
...@@ -11,7 +11,7 @@ router.route("/files/:postId").get(authCtrl.requireLogin, postCtrl.getFiles); ...@@ -11,7 +11,7 @@ router.route("/files/:postId").get(authCtrl.requireLogin, postCtrl.getFiles);
router router
.route("/:postId") .route("/:postId")
.post(authCtrl.requireLogin, postCtrl.addCounts) .post(authCtrl.requireLogin, postCtrl.addCounts)
.delete(authCtrl.requireLogin, postCtrl.deleteOnePost) // +authenticate .delete(authCtrl.requireLogin, authCtrl.authenticate, postCtrl.deleteOnePost)
.put(authCtrl.requireLogin, postCtrl.updateOnePost); .put(authCtrl.requireLogin, postCtrl.updateOnePost);
router.param("postId", postCtrl.userByPostId); router.param("postId", postCtrl.userByPostId);
......
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