From 011c01a741f23734e4154342e9a560925687f152 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Tue, 30 Sep 2025 22:42:46 +0700 Subject: No auto login --- app/lib/api/clearOdooSession.ts | 25 ++++++++ app/login/page.tsx | 45 ++++++++++---- app/page.tsx | 128 +++++++++++++++++----------------------- 3 files changed, 114 insertions(+), 84 deletions(-) create mode 100644 app/lib/api/clearOdooSession.ts diff --git a/app/lib/api/clearOdooSession.ts b/app/lib/api/clearOdooSession.ts new file mode 100644 index 0000000..0ad9a5d --- /dev/null +++ b/app/lib/api/clearOdooSession.ts @@ -0,0 +1,25 @@ +export async function clearOdooSession(baseUrl: string) { + try { + if (baseUrl) { + await fetch(`${baseUrl}/web/session/destroy`, { + method: "POST", + credentials: "include", + headers: { "Content-Type": "application/json" }, + body: "{}", + }); + } + } catch { } + + // 2) hapus cookie session_id di browser + try { + const del = (name: string, domain?: string) => { + const d = domain ? `; domain=${domain}` : ""; + document.cookie = `${name}=; Max-Age=0; Path=/${d}`; + document.cookie = `${name}=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/${d}`; + }; + const host = window.location.hostname.replace(/^www\./, ""); + const parts = host.split("."); + const parent = parts.length >= 2 ? `.${parts.slice(-2).join(".")}` : undefined; + [undefined, host, parent].forEach(dom => del("session_id", dom)); + } catch { } +} diff --git a/app/login/page.tsx b/app/login/page.tsx index c0d7ab8..b4b0ed0 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -16,7 +16,7 @@ import { getAuth, setAuth } from "../lib/api/auth"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { setCookie } from "cookies-next"; - +import { clearOdooSession } from "../lib/api/clearOdooSession"; // Ambil tipe parameter untuk setAuth agar sesuai tepat dengan definisinya type AuthProps = Parameters[0]; @@ -49,10 +49,16 @@ const Login = () => { const [loading, setLoading] = useState(false); useEffect(() => { + void clearOdooSession(process.env.NEXT_PUBLIC_ODOO_API_HOST ?? ""); const token = getAuth(); - if (token) router.push("/"); + if (token) router.replace("/"); }, [router]); + // useEffect(() => { + // const token = getAuth(); + // if (token) router.push("/"); + // }, [router]); + const validateInputs = (e: string, p: string, r: "" | Role) => { let ok = true; @@ -105,7 +111,8 @@ const Login = () => { const rawRole = fd.get("role"); const emailStr = typeof rawEmail === "string" ? rawEmail.trim() : ""; const passwordStr = typeof rawPassword === "string" ? rawPassword : ""; - const roleStr: "" | Role = rawRole === "driver" || rawRole === "dispatch" ? rawRole : ""; + const roleStr: "" | Role = + rawRole === "driver" || rawRole === "dispatch" ? rawRole : ""; if (!validateInputs(emailStr, passwordStr, roleStr)) return; @@ -125,9 +132,8 @@ const Login = () => { if (auth.user && typeof auth.user === "object") { setAuth(auth.user as AuthProps); } - // Simpan pilihan role agar bisa dipakai di halaman lain - setCookie("web_role", roleStr, { path: "/" }); - router.push("/"); + setCookie("web_role", roleStr, { path: "/", sameSite: "lax" }); + router.replace("/"); return; } @@ -140,7 +146,9 @@ const Login = () => { alert("Akun anda belum aktif"); break; default: - alert(res?.status?.description || "Login gagal. Periksa email/password."); + alert( + res?.status?.description || "Login gagal. Periksa email/password." + ); } } catch (error) { console.error(error); @@ -158,7 +166,11 @@ const Login = () => { Sign in @@ -167,13 +179,19 @@ const Login = () => { component="form" onSubmit={handleSubmit} noValidate - sx={{ display: "flex", flexDirection: "column", width: "100%", gap: 2 }} + sx={{ + display: "flex", + flexDirection: "column", + width: "100%", + gap: 2, + }} > Email { { id="role" name="role" value={role} + disabled={loading} onChange={handleRoleChange} displayEmpty size="small" @@ -237,7 +257,12 @@ const Login = () => { )} - diff --git a/app/page.tsx b/app/page.tsx index add141f..af5ada3 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -6,7 +6,14 @@ import SjCamera from "./lib/camera/component/sjCamera"; import DispatchCamera from "./lib/camera/component/dispatchCamera"; import useCameraStore from "./lib/camera/hooks/useCameraStore"; import Header from "./lib/camera/component/hedear"; -import { Button, FormControl, InputLabel, MenuItem, Select, FormHelperText } from "@mui/material"; +import { + Button, + FormControl, + InputLabel, + MenuItem, + Select, + FormHelperText, +} from "@mui/material"; import { SaveAsOutlined } from "@mui/icons-material"; import axios from "axios"; import odooApi from "./lib/api/odooApi"; @@ -14,18 +21,18 @@ import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { getAuth } from "./lib/api/auth"; import { getCookie } from "cookies-next"; +import { clearOdooSession } from "./lib/api/clearOdooSession"; type Role = "driver" | "dispatch"; type ShipMethod = "" | "self_pickup" | "indoteknik_delivery" | "ekspedisi"; export default function Home() { - const [isLogin, setIsLogin] = useState(true); + const [isLogin, setIsLogin] = useState(false); // start false biar nggak nge-flash const [isDriver, setIsDriver] = useState(false); const [isDispatch, setIsDispatch] = useState(false); const [shippingMethod, setShippingMethod] = useState(""); const [shipTouched, setShipTouched] = useState(false); - const { barcode, imageSj, @@ -40,18 +47,20 @@ export default function Home() { const [isLoading, setIsLoading] = useState(false); const router = useRouter(); + // Single effect: auth gate + set role useEffect(() => { const auth = getAuth(); if (!auth) { - router.push("/login"); + void clearOdooSession(process.env.NEXT_PUBLIC_ODOO_API_HOST ?? ""); + router.replace("/login"); return; } - const roleCookie = (getCookie("web_role") as string | undefined)?.toLowerCase() as Role | undefined; - const role: Role | null = roleCookie === "driver" || roleCookie === "dispatch" ? roleCookie : null; - + const roleCookie = ( + getCookie("web_role") as string | undefined + )?.toLowerCase() as Role | undefined; + setIsDriver(roleCookie === "driver"); + setIsDispatch(roleCookie === "dispatch"); setIsLogin(true); - setIsDriver(role === "driver"); - setIsDispatch(role === "dispatch"); }, [router]); const handleSubmit = async (event: React.FormEvent) => { @@ -72,7 +81,6 @@ export default function Home() { return; } - // Validasi foto sesuai role & shipping method if (isDispatch) { if (!imageDispatch) { @@ -80,7 +88,7 @@ export default function Home() { setIsLoading(false); return; } - // SJ opsional untuk self_pickup & ekspedisi → tidak divalidasi + // SJ opsional untuk self_pickup & ekspedisi } else { // Driver: SJ & Penerima wajib if (!imageSj || !imagePackage) { @@ -92,7 +100,9 @@ export default function Home() { try { const newSjImage = imageSj ? imageSj.replace(/^.*?,/, "") : undefined; - const newPackageImage = imagePackage ? imagePackage.replace(/^.*?,/, "") : undefined; + const newPackageImage = imagePackage + ? imagePackage.replace(/^.*?,/, "") + : undefined; const newDispatchImage = imageDispatch && imageDispatch.startsWith("data:") ? imageDispatch.replace(/^.*?,/, "") @@ -107,7 +117,6 @@ export default function Home() { if (submittedSj) data.sj_document = newSjImage!; if (submittedPackage) data.paket_document = newPackageImage!; if (submittedDispatch) data.dispatch_document = newDispatchImage!; - // (opsional) kirim shippingMethod jika backend mau simpan if (isDispatch && shippingMethod) data.shipping_method = shippingMethod; const response = (await odooApi( @@ -141,23 +150,23 @@ export default function Home() { } }; + // === UI helpers === // dispatch: SJ hanya utk self_pickup & ekspedisi const showSjForDispatch = - isDispatch && (shippingMethod === "self_pickup" || shippingMethod === "ekspedisi"); - + isDispatch && + (shippingMethod === "self_pickup" || shippingMethod === "ekspedisi"); // dispatch: kamera dispatch tampil utk semua method (asal sudah dipilih) const showDispatchForDispatch = isDispatch && shippingMethod !== ""; - // preview SJ: sembunyikan kalau dispatch belum pilih method const showSjPreview = !!imageSj && (!isDispatch || showSjForDispatch); - // preview Dispatch: sembunyikan kalau dispatch belum pilih method - const showDispatchPreview = !!imageDispatch && (!isDispatch || showDispatchForDispatch); + const showDispatchPreview = + !!imageDispatch && (!isDispatch || showDispatchForDispatch); return (
- {isLogin && ( + {isLogin ? (
@@ -167,19 +176,32 @@ export default function Home() { {/* Shipping Method (khusus dispatch) */} {isDispatch && (
- - {/* shrink = kunci agar label tidak tumpuk dengan placeholder */} - Shipping Method + + + Shipping Method + {shipTouched && !shippingMethod && ( - Wajib pilih shipping method. + + Wajib pilih shipping method. + )}
)} -
@@ -210,19 +235,16 @@ export default function Home() { <> {showSjForDispatch && } {showDispatchForDispatch && } - {/* dispatch TIDAK perlu kamera penerima */} ) : ( <> {/* driver / non-dispatch */} - {/* driver tidak menampilkan kamera dispatch */} )}
-
{/* Preview SJ */} @@ -271,48 +293,7 @@ export default function Home() {
Captured -
- - )} - - -
- - {!isDispatch && imagePackage && ( - <> - -
- Captured -
- - )} - -
- - {!isDriver && imageDispatch && ( - <> - -
- Captured
- )} - {!isLogin && ( + ) : (

Loading...

-- cgit v1.2.3