generated from wecode-bootcamp-korea/backend-2nd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
회원가입 (아이디, 비번, 이메일 조건 포함) 1차 commit #3
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,90 @@ app.use(morgan('dev')); | |
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
|
||
// 회원가입 | ||
app.post("/users", async (req, res) => { | ||
try { | ||
const me = req.body; | ||
console.log(me); | ||
|
||
const password = me.password; | ||
const email = me.email; | ||
|
||
// key error (필수 입력 정보 없을 경우) | ||
if ( ! nickname || ! password|| ! birthDate || ! email || ! phoneNumber | ||
|| ! gender ) { | ||
const error = new Error("KEY_ERROR"); | ||
error.statusCode = 400; | ||
throw error; | ||
} | ||
|
||
// 이메일 중복 확인, 있으면 에러 | ||
const existingUser = await myDataSource.query(` | ||
SELECT id, email FROM users WHERE email='${email}'; | ||
`); | ||
|
||
console.log("existing user:", existingUser); | ||
if (existingUser.length > 0) { | ||
const error = new Error("이미 존재하는 사용자입니다"); //보안 위해, 이메일 중복임을 밝히지 않음 | ||
error.statusCode = 400; | ||
throw error; | ||
} | ||
|
||
// email . @ 필수 포함 정규식 | ||
const emailRegex = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/; | ||
|
||
if (!emailRegex.test(email)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !emailRegex.test(email) 해당 부분은 |
||
const error = new Error("유효하지 않은 이메일 주소 형식입니다."); | ||
error.statusCode = 400; | ||
throw error; | ||
} | ||
|
||
// 비밀번호 8자리 이상 | ||
if (password.length < 8) { | ||
const error = new Error("패스워드는 8자리 이상이어야 합니다"); | ||
error.statusCode = 400; | ||
throw error; | ||
} | ||
|
||
// DB 저장 전 비밀번호 해시화 | ||
const saltRounds = 10; | ||
const hashedPw = await bcrypt.hash(password, saltRounds); | ||
|
||
|
||
// DB에 회원정보 저장 | ||
const addUser = await myDataSource.query(` | ||
INSERT INTO users ( | ||
nickName, isCheckedMarketing | ||
password, birthDate, | ||
email, phoneNumber, gender, profileImage, provider | ||
) | ||
VALUES ( | ||
'${nickName}', | ||
'${isCheckedMarketing}', | ||
'${password}', | ||
'${birthDate}', | ||
'${email}', | ||
'${phoneNumber}', | ||
'${gender}', | ||
'${profileImage}', | ||
'${provider}' | ||
) | ||
`); | ||
|
||
return res.status(201).json({ | ||
message: "회원가입이 완료되었습니다", | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
return res.status(error.statusCode).json({ | ||
message: "회원가입에 실패하였습니다", | ||
}); | ||
} | ||
}); | ||
|
||
|
||
|
||
app.use((req, _, next) => { | ||
const error = new Error(`${req.method} ${req.url} 라우터가 없습니다.`); | ||
error.status = 404; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const error = new Error("KEY_ERROR");
error.statusCode = 400;
throw error;
해당 부분은
utils
폴더의throwError(400, 'KEY_ERROR')
함수를 사용해서 축약될 수 있을 것 같습니당