diff --git a/src/controller/auth.ts b/src/controller/auth.ts index 33b4dfa..240f5c4 100644 --- a/src/controller/auth.ts +++ b/src/controller/auth.ts @@ -11,6 +11,60 @@ import returnCode from "../library/returnCode"; // services import authService from "../service/auth"; +/** + * @이메일 유효성 검사 + * @route GET /auth/email + * @access public + * @err 1. 필요한 값이 없을 때 + */ +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, + { isValid: false } + ); + } else if (resData === constant.EMAIL_ALREADY_EXIST) { + response.dataResponse( + res, + returnCode.OK, + "이미 사용 중인 이메일입니다.", + true, + { isValid: false } + ); + } else { + response.dataResponse( + res, + returnCode.OK, + "사용할 수 있는 이메일입니다.", + true, + { isValid: 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 +201,7 @@ const postLoginController = async (req: Request, res: Response) => { } }; const authController = { + getEmailController, postSignupController, postLoginController, }; 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); diff --git a/src/service/auth.ts b/src/service/auth.ts index fc6a2e2..a7f3091 100644 --- a/src/service/auth.ts +++ b/src/service/auth.ts @@ -9,6 +9,36 @@ import isEmail from "validator/lib/isEmail"; // models import { User } from "../models"; +/** + * @이메일 유효성 검사 + * @route GET /auth/email + * @access public + * @err 1. 필요한 값이 없을 때 + */ +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 +157,7 @@ const postLoginService = async (email: string, password: string) => { }; const authService = { + getEmailService, postSignupService, postLoginService, };