Files
sori.studio/server/routes/admin/api/auth/login.post.js
zenn cdc16c72b2 태그를 관리용/일반용으로 분리하고 관리자 드래그 정렬을 추가.
댓글/회원/관리자 인증·프로필 흐름 보완과 관련 마이그레이션 및 문서를 함께 반영해 운영 동선을 안정화.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 18:34:23 +09:00

57 lines
1.5 KiB
JavaScript

import { z } from 'zod'
import { createError, readBody } from 'h3'
import bcrypt from 'bcrypt'
import { setAdminSession } from '../../../../utils/admin-auth'
import { getAdminUserByEmail } from '../../../../repositories/member-repository'
import { setMemberSession } from '../../../../utils/member-auth'
const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(1)
})
/**
* 관리자 로그인 API
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {Promise<{ email: string }>} 관리자 세션 정보
*/
export default defineEventHandler(async (event) => {
const parsedBody = loginSchema.safeParse(await readBody(event))
if (!parsedBody.success) {
throw createError({
statusCode: 400,
message: '로그인 요청 형식이 올바르지 않습니다.'
})
}
const body = parsedBody.data
const adminUser = await getAdminUserByEmail(body.email)
const passwordMatched = adminUser
? await bcrypt.compare(body.password, adminUser.passwordHash)
: false
if (!adminUser || !passwordMatched) {
throw createError({
statusCode: 401,
message: '이메일 또는 비밀번호가 올바르지 않습니다.'
})
}
setAdminSession(event, {
userId: adminUser.id,
email: adminUser.email
})
setMemberSession(event, {
userId: adminUser.id,
email: adminUser.email
})
return {
userId: adminUser.id,
email: adminUser.email,
username: adminUser.username
}
})