From a152eaf984e96447e0c41efd89693d82af090833 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 26 Feb 2026 14:12:54 +0800 Subject: [PATCH] Fix Login KodeOtp WA --- src/app/api/auth/login/route.ts | 53 +++++++++++++++----------------- src/app/api/auth/resend/route.ts | 2 +- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts index 18a4a09c..e7b25da4 100644 --- a/src/app/api/auth/login/route.ts +++ b/src/app/api/auth/login/route.ts @@ -33,37 +33,34 @@ export async function POST(req: Request) { const codeOtp = randomOTP(); const otpNumber = Number(codeOtp); - // ✅ PERBAIKAN: Gunakan format pesan yang lebih sederhana - // Hapus karakter khusus yang bisa bikin masalah - // const waMessage = `Website Desa Darmasaba\nKode verifikasi Anda ${codeOtp}`; + const waMessage = `Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`; + const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`; - // // ✅ OPSI 1: Tanpa encoding (coba dulu ini) - // const waUrl = `https://wa.wibudev.com/code?nom=${nomor}&text=${waMessage}`; + console.log("🔍 Debug WA URL:", waUrl); - // ✅ OPSI 2: Dengan encoding (kalau opsi 1 gagal) - // const waUrl = `https://wa.wibudev.com/code?nom=${nomor}&text=${encodeURIComponent(waMessage)}`; - - // ✅ OPSI 3: Encoding manual untuk URL-safe (alternatif terakhir) - // const encodedMessage = waMessage.replace(/\n/g, '%0A').replace(/ /g, '%20'); - // const waUrl = `https://wa.wibudev.com/code?nom=${nomor}&text=${encodedMessage}`; + try { + const res = await fetch(waUrl); + const sendWa = await res.json(); + console.log("📱 WA Response:", sendWa); - // console.log("🔍 Debug WA URL:", waUrl); // Untuk debugging - - // const res = await fetch(waUrl); - // const sendWa = await res.json(); - - // console.log("📱 WA Response:", sendWa); // Debug response - - // if (sendWa.status !== "success") { - // return NextResponse.json( - // { - // success: false, - // message: "Gagal mengirim OTP via WhatsApp", - // debug: sendWa // Tampilkan error detail - // }, - // { status: 400 } - // ); - // } + if (sendWa.status !== "success") { + console.error("❌ WA Service Error:", sendWa); + return NextResponse.json( + { + success: false, + message: "Gagal mengirim OTP via WhatsApp", + debug: sendWa + }, + { status: 400 } + ); + } + } catch (waError) { + console.error("❌ Fetch WA Error:", waError); + return NextResponse.json( + { success: false, message: "Terjadi kesalahan saat mengirim WA" }, + { status: 500 } + ); + } const createOtpId = await prisma.kodeOtp.create({ data: { nomor, otp: otpNumber, isActive: true }, diff --git a/src/app/api/auth/resend/route.ts b/src/app/api/auth/resend/route.ts index dafb5e44..5824057c 100644 --- a/src/app/api/auth/resend/route.ts +++ b/src/app/api/auth/resend/route.ts @@ -19,7 +19,7 @@ export async function POST(req: Request) { const otpNumber = Number(codeOtp); // Kirim OTP via WhatsApp - const waMessage = `Kode verifikasi Anda: ${codeOtp}`; + const waMessage = `Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`; const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`; const waRes = await fetch(waUrl); const waData = await waRes.json();