diff --git a/.env.example b/.env.example index 3208c010..c3b6ee30 100644 --- a/.env.example +++ b/.env.example @@ -11,6 +11,9 @@ SEAFILE_PUBLIC_SHARE_TOKEN=your_seafile_public_share_token WIBU_UPLOAD_DIR=uploads WIBU_DOWNLOAD_DIR=./download +# WhatsApp Server Configuration +WA_SERVER_TOKEN=your_whatsapp_server_token + # Application Configuration # IMPORTANT: For staging/production, set this to your actual domain # Local development: NEXT_PUBLIC_BASE_URL=http://localhost:3000 diff --git a/src/app/api/auth/_lib/sendCodeOtp.ts b/src/app/api/auth/_lib/sendCodeOtp.ts new file mode 100644 index 00000000..96adf9aa --- /dev/null +++ b/src/app/api/auth/_lib/sendCodeOtp.ts @@ -0,0 +1,32 @@ +// app/api/auth/_lib/sendCodeOtp.ts + +const sendCodeOtp = async ({ + nomor, + codeOtp, + newMessage, +}: { + nomor: string; + codeOtp?: string | number; + newMessage?: string; +}) => { + const msg = + newMessage || + `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 enCode = msg; + + const res = await fetch(`https://otp.wibudev.com/api/wa/send-text`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.WA_SERVER_TOKEN}`, + }, + body: JSON.stringify({ + number: nomor, + text: enCode, + }), + }); + + return res; +}; + +export { sendCodeOtp }; diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts index 87d7deb4..90ba89f9 100644 --- a/src/app/api/auth/login/route.ts +++ b/src/app/api/auth/login/route.ts @@ -2,6 +2,7 @@ import prisma from "@/lib/prisma"; import { NextResponse } from "next/server"; import { randomOTP } from "../_lib/randomOTP"; +import { sendCodeOtp } from "../_lib/sendCodeOtp"; import { cookies } from "next/headers"; export async function POST(req: Request) { @@ -35,18 +36,17 @@ export async function POST(req: Request) { console.log(`🔑 DEBUG OTP [${nomor}]: ${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)}`; - - console.log("🔍 Debug WA URL:", waUrl); - try { - const res = await fetch(waUrl); - if (!res.ok) { - console.error(`⚠️ WA Service HTTP Error: ${res.status} ${res.statusText}. Continuing since OTP is logged.`); - console.log(`💡 Use this OTP to login: ${codeOtp}`); + const waResponse = await sendCodeOtp({ + nomor, + codeOtp, + }); + + if (!waResponse.ok) { + console.error(`⚠️ WA Service HTTP Error: ${waResponse.status} ${waResponse.statusText}. Continuing since OTP is logged.`); + console.log(`💡 Use this OTP to login: ${codeOtp}`); } else { - const sendWa = await res.json(); + const sendWa = await waResponse.json(); console.log("📱 WA Response:", sendWa); if (sendWa.status !== "success") { console.error("⚠️ WA Service Logic Error:", sendWa);