diff options
Diffstat (limited to 'app/api/auth/first-auth/route.ts')
| -rw-r--r-- | app/api/auth/first-auth/route.ts | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/app/api/auth/first-auth/route.ts b/app/api/auth/first-auth/route.ts new file mode 100644 index 00000000..18f44904 --- /dev/null +++ b/app/api/auth/first-auth/route.ts @@ -0,0 +1,112 @@ +// /api/auth/first-auth/route.ts +// 1차 인증 처리 API 엔드포인트 + +import { NextRequest, NextResponse } from 'next/server' +import { authHelpers } from '../[...nextauth]/route' + +// 요청 데이터 타입 +interface FirstAuthRequest { + username: string + password: string + provider: 'email' | 'sgips' +} + +// 응답 데이터 타입 +interface FirstAuthResponse { + success: boolean + tempAuthKey?: string + userId?: string + email?: string + error?: string +} + +export async function POST(request: NextRequest): Promise<NextResponse<FirstAuthResponse>> { + try { + // 요청 데이터 파싱 + const body: FirstAuthRequest = await request.json() + const { username, password, provider } = body + + // 입력 검증 + if (!username || !password || !provider) { + return NextResponse.json( + { + success: false, + error: '필수 입력값이 누락되었습니다.' + }, + { status: 400 } + ) + } + + if (!['email', 'sgips'].includes(provider)) { + return NextResponse.json( + { + success: false, + error: '지원하지 않는 인증 방식입니다.' + }, + { status: 400 } + ) + } + + // 레이트 리미팅 (옵셔널) + // const rateLimitResult = await rateLimit.check(request, `first-auth:${username}`) + // if (!rateLimitResult.success) { + // return NextResponse.json( + // { + // success: false, + // error: '너무 많은 시도입니다. 잠시 후 다시 시도해주세요.' + // }, + // { status: 429 } + // ) + // } + + // 1차 인증 수행 + const authResult = await authHelpers.performFirstAuth(username, password, provider) + + if (!authResult.success) { + // 인증 실패 응답 + let errorMessage = '인증에 실패했습니다.' + + if (provider === 'sgips') { + errorMessage = 'S-Gips 계정 정보가 올바르지 않습니다.' + } else { + errorMessage = '이메일 또는 비밀번호가 올바르지 않습니다.' + } + + return NextResponse.json( + { + success: false, + error: authResult.error || errorMessage + }, + { status: 401 } + ) + } + + // 1차 인증 성공 응답 + return NextResponse.json({ + success: true, + tempAuthKey: authResult.tempAuthKey, + userId: authResult.userId, + email: authResult.email + }) + + } catch (error) { + console.error('First auth API error:', error) + + // 에러 응답 + return NextResponse.json( + { + success: false, + error: '서버 오류가 발생했습니다. 잠시 후 다시 시도해주세요.' + }, + { status: 500 } + ) + } +} + +// GET 요청은 지원하지 않음 +export async function GET() { + return NextResponse.json( + { error: 'Method not allowed' }, + { status: 405 } + ) +}
\ No newline at end of file |
