refactor: standarisasi antarmuka fungsi login dan pengambilan OTP

Mengubah parameter fungsi auth_funLogin dan auth_getKodeOtpById di backend untuk menggunakan objek, meningkatkan konsistensi dan memudahkan pemahaman tentang data yang diproses. Selain itu, memperbaiki struktur respons fungsi untuk menambahkan  dalam output, memperjelas sumber terjadinya error.
- Di , fungsi auth_funLogin diubah untuk menerima sebuah objek  daripada hanya .
- Di , perubahan variabel  ke  untuk menyelaraskan dengan parameter terbaru dari fungsi yang diinvokasi.
This commit is contained in:
2024-09-18 14:56:31 +08:00
parent 6ee43ed20f
commit 128dc98839
6 changed files with 26 additions and 27 deletions

View File

@@ -4,8 +4,8 @@ import { ServerEnv } from "@/app/lib/server_env";
export default async function Page({ params }: { params: { id: string } }) {
let kodeOtpId = params.id;
const dataOtp = await auth_getKodeOtpById(kodeOtpId);
let nomor = params.id;
const dataOtp = await auth_getKodeOtpById({nomor: nomor});
return <Validasi dataOtp={dataOtp} />;

View File

@@ -3,7 +3,7 @@
import prisma from "@/app/lib/prisma";
import { randomOTP } from "./rondom_otp";
export async function auth_funLogin(nomor: string) {
export async function auth_funLogin({ nomor }: { nomor: string }) {
const codeOtp = randomOTP();
// console.log(nomor)
@@ -17,7 +17,7 @@ export async function auth_funLogin(nomor: string) {
const sendWa = await res.json();
if (sendWa.status !== "success")
return { status: 400, message: "WA Tidak Terdaftar" };
return { status: 400, message: "WA Tidak Terdaftar", nomorUser: {} };
const createOtpId = await prisma.kodeOtp.create({
data: {
@@ -26,15 +26,16 @@ export async function auth_funLogin(nomor: string) {
},
});
if (!createOtpId) return { status: 400, message: "Gagal Membuat Kode OTP" };
if (!createOtpId)
return { status: 400, message: "Gagal Membuat Kode OTP", nomorUser: {} };
return {
status: 200,
message: "Kode Verifikasi Dikirim",
kodeOtpId: createOtpId.id,
nomorUser: nomor,
};
} catch (error) {
console.log(error);
return { status: 500, message: "Server Error !!!" };
return { status: 500, message: "Server Error !!!", nomorUser: {} };
}
}

View File

@@ -2,10 +2,10 @@
import prisma from "@/app/lib/prisma";
export async function auth_getKodeOtpById(otpId: string) {
export async function auth_getKodeOtpById({nomor}: {nomor: string}) {
const data = await prisma.kodeOtp.findFirst({
where: {
id: otpId,
nomor: nomor,
},
});

View File

@@ -34,16 +34,14 @@ export default function Login() {
const [isError, setError] = useState(false);
async function onLogin() {
const nomorHp = phone.substring(1);
const nomor = phone.substring(1);
if (nomor.length <= 4) return setError(true);
if (nomorHp.length <= 4) return setError(true);
const res = await auth_funLogin(nomorHp);
const res = await auth_funLogin({ nomor: nomor });
if (res.status === 200) {
setLoading(true);
ComponentGlobal_NotifikasiBerhasil(res.message, 2000);
setKodeId(res.kodeOtpId);
router.push(RouterAuth.validasi + res.kodeOtpId);
router.push(RouterAuth.validasi + res.nomorUser, { scroll: false });
} else {
ComponentGlobal_NotifikasiPeringatan(res.message);
}

View File

@@ -12,16 +12,16 @@ export function CheckCookies_UiLayout({
const router = useRouter();
useShallowEffect(() => {
onCheckCookies();
// onCheckCookies();
}, []);
async function onCheckCookies() {
const cek = await fetch("/api/check-cookies");
const result = await cek.json();
if (result.success === false) {
router.push(RouterAuth.login);
}
}
// async function onCheckCookies() {
// const cek = await fetch("/api/check-cookies");
// const result = await cek.json();
// if (result.success === false) {
// router.push(RouterAuth.login);
// }
// }
return <>{children}</>;
}

View File

@@ -13,17 +13,17 @@ const middlewareConfig = {
publicRoute: [
// Daftar route yang bisa diakses tanpa login
"/",
"/auth/login",
"/dev/auth/login",
"/auth/register",
"/api/signin",
"/api/register"
],
publicPatterns: [/^\/api\/files\/\w+/], // Regex buat nge-match route API yang bisa diakses bebas
signinPath: "/auth/login", // Brute buat halaman login
publicPatterns: [/^\/api\/files\/\w+/, /^\/aset\/global\/\w+/], // Regex buat nge-match route API yang bisa diakses bebas
signinPath: "/dev/auth/login", // Brute buat halaman login
userPath: "/user", // Route buat halaman user setelah login
apiRoute: "/api",
tokenKey: "ws_token",
exp: "7 year"
exp: "7 year",
};
// Fungsi buat decode token JWT