From 011c01a741f23734e4154342e9a560925687f152 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Tue, 30 Sep 2025 22:42:46 +0700
Subject: <Miqdad> No auto login

---
 app/login/page.tsx | 45 +++++++++++++++++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 10 deletions(-)

(limited to 'app/login/page.tsx')

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<typeof setAuth>[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 = () => {
           <Typography
             component="h1"
             variant="h4"
-            sx={{ width: "100%", fontSize: "clamp(2rem, 10vw, 2.15rem)", mb: 4 }}
+            sx={{
+              width: "100%",
+              fontSize: "clamp(2rem, 10vw, 2.15rem)",
+              mb: 4,
+            }}
           >
             Sign in
           </Typography>
@@ -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,
+            }}
           >
             <FormControl>
               <FormLabel htmlFor="email">Email</FormLabel>
               <TextField
                 error={emailError}
                 helperText={emailErrorMessage}
+                disabled={loading}
                 id="email"
                 type="email"
                 name="email"
@@ -198,6 +216,7 @@ const Login = () => {
               <TextField
                 error={passwordError}
                 helperText={passwordErrorMessage}
+                disabled={loading}
                 name="password"
                 placeholder="••••••"
                 type="password"
@@ -220,6 +239,7 @@ const Login = () => {
                 id="role"
                 name="role"
                 value={role}
+                disabled={loading}
                 onChange={handleRoleChange}
                 displayEmpty
                 size="small"
@@ -237,7 +257,12 @@ const Login = () => {
               )}
             </FormControl>
 
-            <Button type="submit" fullWidth variant="contained" disabled={loading}>
+            <Button
+              type="submit"
+              fullWidth
+              variant="contained"
+              disabled={loading}
+            >
               {loading ? "Loading..." : "Sign in"}
             </Button>
           </Box>
-- 
cgit v1.2.3

