프로필 수정 업데이트
This commit is contained in:
@@ -164,11 +164,10 @@ exports.getCurrentUser = async (req, res) => {
|
||||
// 프로필 업데이트
|
||||
exports.updateProfile = async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
const { name, email, password, currentPassword } = req.body;
|
||||
const { id, name, email, password, currentPassword } = req.body;
|
||||
|
||||
// 사용자 조회
|
||||
const user = await User.findByPk(userId);
|
||||
const user = await User.findByPk(id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({ message: '사용자를 찾을 수 없습니다.' });
|
||||
@@ -183,33 +182,38 @@ exports.updateProfile = async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 이메일이 변경되는 경우
|
||||
if (email && email !== user.email) {
|
||||
// Users 테이블에서 이메일 중복 확인
|
||||
const existingUser = await User.findOne({ where: { email } });
|
||||
// 이메일이 변경된 경우 중복 확인
|
||||
if (email && email !== user.getDataValue('email')) {
|
||||
// 이메일 유효성 검사 (암호화 전)
|
||||
if (!/^\S+@\S+\.\S+$/.test(email)) {
|
||||
return res.status(400).json({ message: '유효한 이메일 형식이 아닙니다.' });
|
||||
}
|
||||
|
||||
// 이메일 중복 확인 (암호화된 값으로 비교할 수 없으므로 복호화하여 비교)
|
||||
const allUsers = await User.findAll();
|
||||
const existingUser = allUsers.find(u => u.email === email && u.id !== id);
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(400).json({ message: '이미 사용 중인 이메일입니다.' });
|
||||
}
|
||||
|
||||
// Members 테이블에서 이메일 중복 확인
|
||||
const existingMember = await Member.findOne({ where: { email } });
|
||||
if (existingMember) {
|
||||
return res.status(400).json({ message: '이미 클럽 회원이 사용 중인 이메일입니다.' });
|
||||
}
|
||||
|
||||
// 해당 사용자의 Members 테이블 레코드도 함께 업데이트
|
||||
await Member.update(
|
||||
{ email },
|
||||
{ where: { userId } }
|
||||
{ email, name },
|
||||
{ where: { userId: id } }
|
||||
);
|
||||
}
|
||||
|
||||
// 프로필 업데이트
|
||||
const updateData = {
|
||||
name: name || user.name,
|
||||
email: email || user.email
|
||||
name: name || user.name
|
||||
// username은 변경하지 않음 - 로그인 식별자로 사용되기 때문
|
||||
};
|
||||
|
||||
// 이메일은 별도로 처리 (암호화되어 있기 때문에)
|
||||
if (email) {
|
||||
updateData.email = email;
|
||||
}
|
||||
|
||||
// 비밀번호가 제공된 경우에만 업데이트
|
||||
if (password && currentPassword) {
|
||||
@@ -219,7 +223,7 @@ exports.updateProfile = async (req, res) => {
|
||||
await user.update(updateData);
|
||||
|
||||
// 업데이트된 사용자 정보 반환 (비밀번호 제외)
|
||||
const updatedUser = await User.findByPk(userId, {
|
||||
const updatedUser = await User.findByPk(id, {
|
||||
attributes: { exclude: ['password'] }
|
||||
});
|
||||
|
||||
|
||||
@@ -30,9 +30,6 @@ const User = sequelize.define('User', {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
validate: {
|
||||
isEmail: true
|
||||
},
|
||||
comment: '이메일 (암호화됨)',
|
||||
get() {
|
||||
const rawValue = this.getDataValue('email');
|
||||
|
||||
Reference in New Issue
Block a user