Files
desa-darmasaba/src/app/api/auth/register/route.ts
nico 781d125d4c feat(auth): migrate WhatsApp OTP to otp.wibudev.com with API Key authentication
- Create new wa-service.ts helper library
  - sendWhatsAppOtp(): Send OTP via otp.wibudev.com with Bearer token auth
  - sendWhatsAppOtpLegacy(): Deprecated legacy function for backward compat
  - Proper error handling and response validation

- Update all auth routes to use new WA service:
  - login/route.ts: Use sendWhatsAppOtp for login OTP
  - register/route.ts: Use sendWhatsAppOtp for registration OTP
  - resend/route.ts: Use sendWhatsAppOtp for resend OTP
  - send-otp-register/route.ts: Use sendWhatsAppOtp for registration

- Add environment variables to .env.local:
  - WIBU_WA_API_KEY: JWT token for authentication
  - WIBU_WA_API_URL: https://otp.wibudev.com

Benefits:
✓ Secure authentication with JWT API Key
✓ Centralized WA service for all OTP sending
✓ Better error handling and logging
✓ Consistent API response format
✓ Easy to maintain and extend

API Key Info:
- Name: website-desa-darmasaba
- Description: untuk website desa darmasaba
- Expiration: Feb 12, 2116
- Issued: Mar 05, 2026

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-03-05 12:07:58 +08:00

69 lines
2.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
import prisma from '@/lib/prisma';
import { randomOTP } from '../_lib/randomOTP';
import { sendWhatsAppOtp } from '@/lib/wa-service';
export async function POST(req: Request) {
try {
const { username, nomor } = await req.json();
if (!username || !nomor) {
return NextResponse.json({ success: false, message: 'Data tidak lengkap' }, { status: 400 });
}
// Cek duplikat
if (await prisma.user.findUnique({ where: { nomor } })) {
return NextResponse.json({ success: false, message: 'Nomor sudah terdaftar' }, { status: 409 });
}
if (await prisma.user.findFirst({ where: { username } })) {
return NextResponse.json({ success: false, message: 'Username sudah digunakan' }, { status: 409 });
}
// ✅ Generate dan kirim OTP
const codeOtp = randomOTP();
const otpNumber = Number(codeOtp);
const waMessage = `Website Desa Darmasaba - Kode verifikasi Anda: ${codeOtp}`;
// Send OTP via WhatsApp using authenticated API
const waResult = await sendWhatsAppOtp({
nomor,
message: waMessage,
});
if (!waResult.success) {
return NextResponse.json(
{ success: false, message: waResult.message || 'Gagal mengirim OTP via WhatsApp', debug: waResult.data },
{ status: 400 }
);
}
// ✅ Simpan OTP ke database
const otpRecord = await prisma.kodeOtp.create({
data: { nomor, otp: otpNumber, isActive: true }
});
// ✅ Set cookie flow=register (Next.js 15+ syntax)
const cookieStore = await cookies();
cookieStore.set('auth_flow', 'register', {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 60 * 5, // 5 menit
path: '/'
});
// ✅ Kembalikan kodeId
return NextResponse.json({
success: true,
message: 'Kode verifikasi dikirim',
kodeId: otpRecord.id,
});
} catch (error) {
console.error('Register OTP Error:', error);
return NextResponse.json({ success: false, message: 'Gagal mengirim OTP' }, { status: 500 });
} finally {
await prisma.$disconnect();
}
}