Files
jaengseung-made/app/api/telegram/connect/route.ts
gahusb a95715ec6b feat: 로또 추천 API, 텔레그램 봇 연동, 관리자 페이지 추가
- 로또 번호 추천 구독자 전용 페이지 (/services/lotto/recommend)
- NAS 몬테카를로 API 연동 + 클라이언트 사이드 폴백
- 무료 미리보기 1개 + 구독자용 프리미엄 번호 추천
- 구독 플랜 변경: 골드(900원)/플래티넘(2,900원)/다이아(9,900원)
- 텔레그램 봇 연동: 연결/해제, 웹훅, /start 명령 처리
- 마이페이지 텔레그램 연결 UI + 가이드 모달
- 관리자 페이지 (/admin): 대시보드, 회원, 서비스, 문의 관리
- Supabase 마이그레이션: profiles 텔레그램 컬럼, 신규 상품

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-16 02:12:17 +09:00

77 lines
2.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase/server';
/**
* POST /api/telegram/connect
* 인증된 유저에게 15분 유효 연결 토큰을 발급하고
* 텔레그램 봇 딥링크를 반환합니다.
*
* Response: { deepLink: string, expiresAt: string }
*/
export async function POST() {
const supabase = await createClient();
const { data: { user }, error: authError } = await supabase.auth.getUser();
if (authError || !user) {
return NextResponse.json({ error: 'UNAUTHORIZED' }, { status: 401 });
}
const botUsername = process.env.TELEGRAM_BOT_USERNAME;
if (!botUsername) {
return NextResponse.json({ error: 'TELEGRAM_BOT_USERNAME이 설정되지 않았습니다.' }, { status: 500 });
}
// 15분 유효 토큰 생성
const token = crypto.randomUUID().replace(/-/g, '');
const expiresAt = new Date(Date.now() + 15 * 60 * 1000).toISOString();
// 프로필 upsert (없는 경우 대비)
await supabase
.from('profiles')
.upsert({ id: user.id, email: user.email }, { onConflict: 'id' });
const { error: updateError } = await supabase
.from('profiles')
.update({
telegram_connect_token: token,
telegram_token_expires: expiresAt,
})
.eq('id', user.id);
if (updateError) {
console.error('telegram connect token update error:', updateError);
return NextResponse.json({ error: 'DB_ERROR' }, { status: 500 });
}
const deepLink = `https://t.me/${botUsername}?start=${token}`;
return NextResponse.json({ deepLink, expiresAt });
}
/**
* DELETE /api/telegram/connect
* 텔레그램 연결 해제 (chat_id 및 토큰 초기화)
*/
export async function DELETE() {
const supabase = await createClient();
const { data: { user }, error: authError } = await supabase.auth.getUser();
if (authError || !user) {
return NextResponse.json({ error: 'UNAUTHORIZED' }, { status: 401 });
}
const { error } = await supabase
.from('profiles')
.update({
telegram_chat_id: null,
telegram_connect_token: null,
telegram_token_expires: null,
})
.eq('id', user.id);
if (error) {
return NextResponse.json({ error: 'DB_ERROR' }, { status: 500 });
}
return NextResponse.json({ ok: true });
}