From 775d915fe9ee0d3f16515556e05999d570a94e87 Mon Sep 17 00:00:00 2001 From: Daeki Yoon Date: Thu, 7 Jul 2022 15:47:40 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B9=84=EB=B0=80=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EC=95=94=ED=98=B8=ED=99=94=EB=A5=BC=20db=EC=AA=BD=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94=20=EA=B2=83?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/auth.controller.ts | 23 ++++++++++++----------- src/db/user.db.ts | 5 ++++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index b1b2ea5..f980583 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -25,15 +25,17 @@ export const login = asyncWrap(async (req, res) => { return res.status(401).send("잘못된 비밀번호를 입력하셨습니다"); } // 3) 비밀번호가 맞으면 토큰 생성 - const token = jwt.sign({ userId: user.id }, jwtCofig.secret, { //userId를 토큰에다 넣는 중. + const token = jwt.sign({ userId: user.id }, jwtCofig.secret, { + //userId를 토큰에다 넣는 중. expiresIn: jwtCofig.expires, }); // 4) 토큰을 쿠키에 저장 - res.cookie(cookieConfig.name, token, {//token은 쿠키에 무엇을 실렸는가 이다. 항상 갖고 있다가 홈페이지 들어가면 서버로 접속 - maxAge: cookieConfig.maxAge,// 이 기간 한에서만 유효 - path: "/",//어떠한 경로에 관해서만 쓴다. 지금은 전부에 쓴다. - httpOnly: envConfig.mode === "production", //false 면 브라우저에서 쿠키를 조작, true면 조작할 수 있다. - secure: envConfig.mode === "production", //true 면 https 를 통해서만 쿠키 전달, false면 + res.cookie(cookieConfig.name, token, { + //token은 쿠키에 무엇을 실렸는가 이다. 항상 갖고 있다가 홈페이지 들어가면 서버로 접속 + maxAge: cookieConfig.maxAge, // 이 기간 내에서만 유효 + path: "/", //어떠한 경로에 관해서만 쓴다. 지금은 전부에 쓴다. + httpOnly: envConfig.mode === "production", //false면 브라우저에서 쿠키를 조작, true면 조작할 수 없다. + secure: envConfig.mode === "production", //true 면 https를 통해서만 쿠키 전달, false면 }); // 5) 사용자 반환 res.json({ @@ -59,7 +61,7 @@ export const requireLogin = asyncWrap(async (reqExp, res, next) => { const decodedUser = jwt.verify(token, jwtCofig.secret); // 아까보낸 토근을 디코딩중. // 3) 요청 객체에 토큰 사용자 객체 추가 req.auth = decodedUser; - next();// 에러가 안나오면 next 사용, 나오면 catch쪽으로. + next(); // 에러가 안나오면 next 사용, 나오면 catch쪽으로. } catch (error) { res.clearCookie(cookieConfig.name); console.log("error in requreLogin===\n", error); @@ -84,13 +86,12 @@ export const signup = asyncWrap(async (req, res) => { if (userExist) { return res.status(422).send(`${email} 사용자가 이미 존재합니다`); } - // 3) 비밀번호 암호화 - const hash = await bcrypt.hash(password, 10); + // 3) 비밀번호 암호화는 useDb.createUser에서 처리 // 4) 새로운 사용자 만들기 const newUser = await userDb.createUser({ email, - password: hash, + password, }); - // 5) 사용자 반환 + // 5) 사용자 반환(내부적으로 몽구스가 toJSON() 호출) res.json(newUser); }); diff --git a/src/db/user.db.ts b/src/db/user.db.ts index a7c794c..e32309a 100644 --- a/src/db/user.db.ts +++ b/src/db/user.db.ts @@ -1,7 +1,10 @@ +import bcrypt from "bcryptjs"; import { IUser, User } from "../models"; export const createUser = async (user: IUser) => { - const newUser = await User.create(user); + // 비밀번호 암호화 + const hash = await bcrypt.hash(user.password, 10); + const newUser = await User.create({ email: user.email, password: hash }); return newUser; }; -- GitLab