diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-20 10:32:53 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-20 10:32:53 +0700 |
| commit | bf8b6aacbbfef3df4b56be5e63e8f983d71f9c7c (patch) | |
| tree | 2f45681975767399fe2ca855077e6dc928f9931c /app/login | |
| parent | 5c3d6cfd6f68b6eb7192aba47463bd0541bfbf48 (diff) | |
<Miqdad> fix compile erroradd_role
Diffstat (limited to 'app/login')
| -rw-r--r-- | app/login/page.tsx | 223 |
1 files changed, 67 insertions, 156 deletions
diff --git a/app/login/page.tsx b/app/login/page.tsx index 5ad699c..9e0a5fe 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -1,173 +1,84 @@ "use client"; -import { - Box, - Button, - FormControl, - FormLabel, - TextField, - Typography, -} from "@mui/material"; -import Header from "../lib/camera/component/hedear"; -import odooApi from "../lib/api/odooApi"; -import { getAuth, setAuth } from "../lib/api/auth"; + import { useRouter } from "next/navigation"; -import { useEffect, useState } from "react"; +import { useState } from "react"; +import odooApi from "../lib/api/odooApi"; + +type LoginStatus = { code?: number; description?: string }; +type LoginResult = { token?: string; email?: string; [k: string]: unknown }; +type LoginResponse = { status?: LoginStatus; result?: LoginResult }; -const Login = () => { +export default function LoginPage() { const router = useRouter(); - const [emailError, setEmailError] = useState(false); - const [emailErrorMessage, setEmailErrorMessage] = useState(""); - const [passwordError, setPasswordError] = useState(false); - const [passwordErrorMessage, setPasswordErrorMessage] = useState(""); + const [loading, setLoading] = useState(false); - useEffect(() => { - const token = getAuth(); + const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => { + e.preventDefault(); + setLoading(true); - if (token) { - router.push("/"); - } - }, [router]); + const fd = new FormData(e.currentTarget); + + // Narrowing ke string, bukan File/null + const rawEmail = fd.get("email"); + const rawPassword = fd.get("password"); + const email = typeof rawEmail === "string" ? rawEmail.trim() : ""; + const password = typeof rawPassword === "string" ? rawPassword : ""; - const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { - event.preventDefault(); - if (emailError || passwordError) { + if (!email || !password) { + alert("Email dan password wajib diisi."); + setLoading(false); return; } - const data = new FormData(event.currentTarget); - const email = data.get("email"); - const password = data.get("password"); - + try { - odooApi("POST", "/api/v1/user/login", { - email , - password - }).then((res) => { - const auth = res.result; - if (auth.is_auth) { - setAuth(auth.user); - router.push("/"); - return; - } - switch (auth.reason) { - case "NOT_FOUND": - alert("Email tidak ditemukan"); - break; - case "NOT_ACTIVE": - alert("Akun anda belum aktif"); - break; - } - }); - } catch (error) { - alert("Gagal login, silahkan coba lagi"); - console.log(error); + const res = (await odooApi("POST", "/api/v1/user/login", { + email, + password, + })) as unknown as LoginResponse; + + if (res?.status?.code === 200) { + // Jika kamu punya util setAuth(res.result), panggil di sini. + router.push("/"); + } else { + alert(res?.status?.description || "Login gagal. Periksa email/password."); + } + } catch (err) { + console.error(err); + alert("Terjadi kesalahan saat login."); + } finally { + setLoading(false); } }; - const validateInputs = () => { - const email = document.getElementById("email") as HTMLInputElement; - const password = document.getElementById("password") as HTMLInputElement; - - let isValid = true; + return ( + <main className="min-h-screen flex items-center justify-center"> + <form onSubmit={onSubmit} className="w-full max-w-sm p-6 space-y-3 border rounded"> + <h1 className="text-xl font-semibold">Login</h1> - if (!email.value || !/\S+@\S+\.\S+/.test(email.value)) { - setEmailError(true); - setEmailErrorMessage("Please enter a valid email address."); - isValid = false; - } else { - setEmailError(false); - setEmailErrorMessage(""); - } + <input + name="email" + type="email" + placeholder="Email" + className="w-full border rounded px-3 py-2" + required + /> - if (!password.value || password.value.length < 6) { - setPasswordError(true); - setPasswordErrorMessage("Password must be at least 6 characters long."); - isValid = false; - } else { - setPasswordError(false); - setPasswordErrorMessage(""); - } + <input + name="password" + type="password" + placeholder="Password" + className="w-full border rounded px-3 py-2" + required + /> - return isValid; - }; - return ( - <div className="bg-[#fafeff] h-screen overflow-auto"> - <Header /> - <div className="py-4 px-4 sm:px-96 pt-20"> - <div className="bg-white py-6 px-4 sm:px-96 shadow-md rounded-sm"> - <Typography - component="h1" - variant="h4" - sx={{ - width: "100%", - fontSize: "clamp(2rem, 10vw, 2.15rem)", - mb: 4, - }} - > - Sign in - </Typography> - <Box - component="form" - onSubmit={handleSubmit} - noValidate - sx={{ - display: "flex", - flexDirection: "column", - width: "100%", - gap: 2, - }} - > - <FormControl> - <FormLabel htmlFor="email">Email</FormLabel> - <TextField - error={emailError} - helperText={emailErrorMessage} - id="email" - type="email" - name="email" - placeholder="your@email.com" - autoComplete="email" - autoFocus - required - fullWidth - variant="outlined" - color={emailError ? "error" : "primary"} - sx={{ ariaLabel: "email" }} - size="small" - /> - </FormControl> - <FormControl> - <Box sx={{ display: "flex", justifyContent: "space-between" }}> - <FormLabel htmlFor="password">Password</FormLabel> - </Box> - <TextField - error={passwordError} - helperText={passwordErrorMessage} - name="password" - placeholder="••••••" - type="password" - id="password" - autoComplete="current-password" - autoFocus - required - fullWidth - variant="outlined" - color={passwordError ? "error" : "primary"} - size="small" - /> - </FormControl> - <Button - type="submit" - fullWidth - variant="contained" - onClick={validateInputs} - > - Sign in - </Button> - </Box> - </div> - </div> - </div> + <button + type="submit" + disabled={loading} + className="w-full bg-red-600 text-white rounded py-2" + > + {loading ? "Loading..." : "Login"} + </button> + </form> + </main> ); -}; - -export default Login; +}
\ No newline at end of file |
