Skip to content
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 4 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

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') 함수를 사용해서 축약될 수 있을 것 같습니당

}

// 이메일 중복 확인, 있으면 에러
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)) {
Copy link
Contributor

@03290419 03290419 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!emailRegex.test(email)

해당 부분은 utils 폴더의 isValidData(emailRegex, 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;
Expand Down