From 37dfa352d159766c6ae9caf5df1859d513516df8 Mon Sep 17 00:00:00 2001 From: donggeun Date: Sat, 15 Jan 2022 22:08:49 +0900 Subject: [PATCH 1/5] feat: add /auth/email route --- src/router/auth.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/router/auth.ts b/src/router/auth.ts index 70d754d..803733c 100644 --- a/src/router/auth.ts +++ b/src/router/auth.ts @@ -4,6 +4,7 @@ const router = express.Router(); // Controller import authController from "../controller/auth"; +router.get("/email", authController.getEmailController); router.post("/login", authController.postLoginController); router.post("/signup", authController.postSignupController); From f7fe793af650a4e7c9d99abe4e28794c5051a63e Mon Sep 17 00:00:00 2001 From: donggeun Date: Sat, 15 Jan 2022 22:09:10 +0900 Subject: [PATCH 2/5] feat: add getEmailController --- src/controller/auth.ts | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/controller/auth.ts b/src/controller/auth.ts index 33b4dfa..fd3d88e 100644 --- a/src/controller/auth.ts +++ b/src/controller/auth.ts @@ -11,6 +11,68 @@ import returnCode from "../library/returnCode"; // services import authService from "../service/auth"; +/** + * @이메일 유효성 검사 + * @route GET /auth/email + * @access public + * @err 1. 필요한 값이 없을 때 + * 2. 이메일 형식이 올바르지 않을 때 + * 3. 이메일이 이미 존재할 때 + */ +const getEmailController = async (req: Request, res: Response) => { + try { + const resData: number = await authService.getEmailService(req.body.email); + + if (resData === constant.NULL_VALUE) { + response.basicResponse( + res, + returnCode.BAD_REQUEST, + false, + "필요한 값이 없습니다." + ); + } else if (resData === constant.WRONG_EMAIL_CONVENTION) { + response.dataResponse( + res, + returnCode.OK, + "올바른 형식이 아닙니다.", + true, + { + isUnique: false, + } + ); + } else if (resData === constant.EMAIL_ALREADY_EXIST) { + response.dataResponse( + res, + returnCode.OK, + "이미 사용 중인 이메일입니다.", + true, + { + isUnique: false, + } + ); + } else { + response.dataResponse( + res, + returnCode.OK, + "사용할 수 있는 이메일입니다.", + true, + { + isUnique: true, + } + ); + } + } catch (err) { + slack.slackWebhook(req, err.message); + console.error(err.message); + response.basicResponse( + res, + returnCode.INTERNAL_SERVER_ERROR, + false, + "서버 오류" + ); + } +}; + /** * @회원가입 * @route POST /auth/signup @@ -147,6 +209,7 @@ const postLoginController = async (req: Request, res: Response) => { } }; const authController = { + getEmailController, postSignupController, postLoginController, }; From df60eade16c45af597ec88560c40275752b4b542 Mon Sep 17 00:00:00 2001 From: donggeun Date: Sat, 15 Jan 2022 22:09:22 +0900 Subject: [PATCH 3/5] feat: add getEmailService --- src/service/auth.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/service/auth.ts b/src/service/auth.ts index fc6a2e2..e194e3e 100644 --- a/src/service/auth.ts +++ b/src/service/auth.ts @@ -9,6 +9,38 @@ import isEmail from "validator/lib/isEmail"; // models import { User } from "../models"; +/** + * @이메일 유효성 검사 + * @route GET /auth/email + * @access public + * @err 1. 필요한 값이 없을 때 + * 2. 이메일 형식이 올바르지 않을 때 + * 3. 이메일이 이미 존재할 때 + */ +const getEmailService = async (email: string) => { + // 필요한 값이 존재하지 않는 경우 + if (!email) { + return constant.NULL_VALUE; + } + + // email 형식이 잘못되었을 때 + if (!isEmail(email)) { + return constant.WRONG_EMAIL_CONVENTION; + } + + // email이 이미 존재할 때 + const emailExist = await User.findAll({ + where: { + email, + }, + }); + if (emailExist.length > 0) { + return constant.EMAIL_ALREADY_EXIST; + } + + return constant.SUCCESS; +}; + /** * @회원가입 * @route POST /auth/signup @@ -127,6 +159,7 @@ const postLoginService = async (email: string, password: string) => { }; const authService = { + getEmailService, postSignupService, postLoginService, }; From 6993afd9fb1b9bf8adba0f723d6f3db73dc8849c Mon Sep 17 00:00:00 2001 From: donggeun Date: Sat, 15 Jan 2022 22:37:17 +0900 Subject: [PATCH 4/5] chore: delete useless comment, change code style --- src/controller/auth.ts | 14 +++----------- src/service/auth.ts | 2 -- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/controller/auth.ts b/src/controller/auth.ts index fd3d88e..07f827b 100644 --- a/src/controller/auth.ts +++ b/src/controller/auth.ts @@ -16,8 +16,6 @@ import authService from "../service/auth"; * @route GET /auth/email * @access public * @err 1. 필요한 값이 없을 때 - * 2. 이메일 형식이 올바르지 않을 때 - * 3. 이메일이 이미 존재할 때 */ const getEmailController = async (req: Request, res: Response) => { try { @@ -36,9 +34,7 @@ const getEmailController = async (req: Request, res: Response) => { returnCode.OK, "올바른 형식이 아닙니다.", true, - { - isUnique: false, - } + { isUnique: false } ); } else if (resData === constant.EMAIL_ALREADY_EXIST) { response.dataResponse( @@ -46,9 +42,7 @@ const getEmailController = async (req: Request, res: Response) => { returnCode.OK, "이미 사용 중인 이메일입니다.", true, - { - isUnique: false, - } + { isUnique: false } ); } else { response.dataResponse( @@ -56,9 +50,7 @@ const getEmailController = async (req: Request, res: Response) => { returnCode.OK, "사용할 수 있는 이메일입니다.", true, - { - isUnique: true, - } + { isUnique: true } ); } } catch (err) { diff --git a/src/service/auth.ts b/src/service/auth.ts index e194e3e..a7f3091 100644 --- a/src/service/auth.ts +++ b/src/service/auth.ts @@ -14,8 +14,6 @@ import { User } from "../models"; * @route GET /auth/email * @access public * @err 1. 필요한 값이 없을 때 - * 2. 이메일 형식이 올바르지 않을 때 - * 3. 이메일이 이미 존재할 때 */ const getEmailService = async (email: string) => { // 필요한 값이 존재하지 않는 경우 From 9dd9941d028ecfe22ab44f7fe1563f38207ebe6b Mon Sep 17 00:00:00 2001 From: donggeun Date: Sat, 15 Jan 2022 22:44:39 +0900 Subject: [PATCH 5/5] chore: change return data key name --- src/controller/auth.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controller/auth.ts b/src/controller/auth.ts index 07f827b..240f5c4 100644 --- a/src/controller/auth.ts +++ b/src/controller/auth.ts @@ -34,7 +34,7 @@ const getEmailController = async (req: Request, res: Response) => { returnCode.OK, "올바른 형식이 아닙니다.", true, - { isUnique: false } + { isValid: false } ); } else if (resData === constant.EMAIL_ALREADY_EXIST) { response.dataResponse( @@ -42,7 +42,7 @@ const getEmailController = async (req: Request, res: Response) => { returnCode.OK, "이미 사용 중인 이메일입니다.", true, - { isUnique: false } + { isValid: false } ); } else { response.dataResponse( @@ -50,7 +50,7 @@ const getEmailController = async (req: Request, res: Response) => { returnCode.OK, "사용할 수 있는 이메일입니다.", true, - { isUnique: true } + { isValid: true } ); } } catch (err) {