summaryrefslogtreecommitdiff
path: root/app/api/auth/first-auth/route.ts
blob: 18f4490416a7d4917754ac6837032da6cb93dbc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 }
  )
}