Skip to content

Commit

Permalink
Merge pull request #51 from TeamBookTez/feat/nickname-api
Browse files Browse the repository at this point in the history
Feat/nickname api
  • Loading branch information
seohyun-106 authored Jan 15, 2022
2 parents 8813235 + 005f0dc commit 656fcfb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/controller/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,66 @@ const postLoginController = async (req: Request, res: Response) => {
);
}
};

/**
* @닉네임_유효성_검사
* @route get auth/nickname
* @access public
* @err 1. 필요한 값이 없습니다.
*/

const getNicknameController = async (req: Request, res: Response) => {
try {
const resData = await authService.getNicknameService(req.body.nickname);

if (resData === constant.NULL_VALUE) {
response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"필요한 값이 없습니다."
);
} else if (resData === constant.WRONG_NICKNAME_CONVENTION) {
response.dataResponse(
res,
returnCode.OK,
"올바른 형식이 아닙니다.",
true,
{ isValid: false }
);
} else if (resData === constant.NICKNAME_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,
"서버 오류"
);
}
}
};

const authController = {
getEmailController,
getNicknameController,
postSignupController,
postLoginController,
};
Expand Down
1 change: 1 addition & 0 deletions src/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const getMyInfoController = async (req: Request, res: Response) => {
const patchImgController = async (req: Request, res: Response) => {
try {
const img = req.file.location ? req.file.location : null;

const resData = await userService.patchImgService(req.body.userID.id, img);

// 폼데이터 잘못된 경우
Expand Down
1 change: 1 addition & 0 deletions src/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const router = express.Router();
import authController from "../controller/auth";

router.get("/email", authController.getEmailController);
router.get("/nickname", authController.getNicknameController);
router.post("/login", authController.postLoginController);
router.post("/signup", authController.postSignupController);

Expand Down
35 changes: 35 additions & 0 deletions src/service/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,42 @@ const postLoginService = async (email: string, password: string) => {
return { nickname, token };
};

/**
* @닉네임_유효성_검사
* @route get auth/nickname
* @access public
* @err 1. 필요한 값이 없습니다.
*/

const getNicknameService = async (nickname: string) => {
// 필요한 값이 존재하지 않는 경우
if (!nickname) {
return constant.NULL_VALUE;
}

if (
// nickname 형식이 잘못되었을 때
!/^[ㄱ-ㅎ|가-힣|a-z|A-Z|0-9|]+$/.test(nickname) ||
nickname.length < 2 ||
nickname.length > 8
) {
return constant.WRONG_NICKNAME_CONVENTION;
}

// nickname이 이미 존재할 때
const nicknameExist = await User.findAll({
where: {
nickname,
},
});

if (nicknameExist.length > 0) {
return constant.NICKNAME_ALREADY_EXIST;
}
};

const authService = {
getNicknameService,
getEmailService,
postSignupService,
postLoginService,
Expand Down

0 comments on commit 656fcfb

Please sign in to comment.