summaryrefslogtreecommitdiff
path: root/app/api/auth/send-sms/route.ts
blob: 6b9eb114e45d3fc24ee702a04df3af6111e414a0 (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
// app/api/auth/send-sms/route.ts

import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { getUserByEmail, getUserById } from '@/lib/users/repository';
import { generateAndSendSmsToken } from '@/lib/users/auth/passwordUtil';

const sendSmsSchema = z.object({
  userId: z.string(),
});

export async function POST(request: NextRequest) {
  try {

    const body = await request.json();
    const { userId } = sendSmsSchema.parse(body);

    console.log(userId, "userId")

    // 본인 확인
    if (!userId) {
      return NextResponse.json(
        { error: '권한이 없습니다' },
        { status: 403 }
      );
    }

    // 사용자 정보 조회
    const user = await getUserById(Number(userId));
    if (!user || !user.phone) {
      return NextResponse.json(
        { error: '전화번호가 등록되지 않았습니다' },
        { status: 400 }
      );
    }

    console.log(user, "user")



    // SMS 전송
    const result = await generateAndSendSmsToken(Number(userId), user.phone);
    
    if (result.success) {
      return NextResponse.json({ 
        success: true,
        message: 'SMS가 전송되었습니다' 
      });
    } else {
      return NextResponse.json(
        { error: result.error },
        { status: 400 }
      );
    }

  } catch (error) {
    if (error instanceof z.ZodError) {
      return NextResponse.json(
        { error: '잘못된 요청입니다' },
        { status: 400 }
      );
    }

    console.error('SMS send API error:', error);
    return NextResponse.json(
      { error: '서버 오류가 발생했습니다' },
      { status: 500 }
    );
  }
}