v0.1.18 - 설정 화면과 기간형 D-DAY 관리 추가

This commit is contained in:
2026-04-22 09:47:04 +09:00
parent 4355185203
commit fe538fc88b
13 changed files with 1033 additions and 173 deletions

View File

@@ -16,6 +16,16 @@ const loginSchema = z.object({
password: z.string().min(1).max(72),
})
const profileSchema = z.object({
email: z.string().trim().email(),
nickname: z.string().trim().min(2).max(30),
})
const passwordSchema = z.object({
currentPassword: z.string().min(1).max(72),
newPassword: z.string().min(8).max(72),
})
function sanitizeUser(user) {
return {
id: user.id,
@@ -129,4 +139,93 @@ export async function registerAuthRoutes(app) {
user: sanitizeUser(user),
}
})
app.put('/api/auth/profile', async (request, reply) => {
const user = await findAuthenticatedUser(request)
if (!user) {
return reply.code(401).send({
message: '인증이 필요합니다.',
})
}
const payload = profileSchema.safeParse(request.body)
if (!payload.success) {
return reply.code(400).send({
message: '프로필 입력값이 올바르지 않습니다.',
issues: payload.error.flatten(),
})
}
const normalizedEmail = payload.data.email.toLowerCase()
const [existingUser] = await db
.select()
.from(users)
.where(eq(users.email, normalizedEmail))
.limit(1)
if (existingUser && existingUser.id !== user.id) {
return reply.code(409).send({
message: '이미 사용 중인 이메일입니다.',
})
}
const [updatedUser] = await db
.update(users)
.set({
email: normalizedEmail,
nickname: payload.data.nickname,
updatedAt: new Date(),
})
.where(eq(users.id, user.id))
.returning()
return {
message: '프로필이 수정되었습니다.',
user: sanitizeUser(updatedUser),
}
})
app.put('/api/auth/password', async (request, reply) => {
const user = await findAuthenticatedUser(request)
if (!user) {
return reply.code(401).send({
message: '인증이 필요합니다.',
})
}
const payload = passwordSchema.safeParse(request.body)
if (!payload.success) {
return reply.code(400).send({
message: '비밀번호 입력값이 올바르지 않습니다.',
issues: payload.error.flatten(),
})
}
const passwordMatches = await verifyPassword(payload.data.currentPassword, user.passwordHash)
if (!passwordMatches) {
return reply.code(401).send({
message: '현재 비밀번호가 올바르지 않습니다.',
})
}
const passwordHash = await hashPassword(payload.data.newPassword)
await db
.update(users)
.set({
passwordHash,
updatedAt: new Date(),
})
.where(eq(users.id, user.id))
return {
message: '비밀번호가 변경되었습니다.',
}
})
}