"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"; const Login = () => { const router = useRouter(); const [emailError, setEmailError] = useState(false); const [emailErrorMessage, setEmailErrorMessage] = useState(""); const [passwordError, setPasswordError] = useState(false); const [passwordErrorMessage, setPasswordErrorMessage] = useState(""); useEffect(() => { const token = getAuth(); if (token) { router.push("/"); } }, [router]); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (emailError || passwordError) { 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 validateInputs = () => { const email = document.getElementById("email") as HTMLInputElement; const password = document.getElementById("password") as HTMLInputElement; let isValid = true; if (!email.value || !/\S+@\S+\.\S+/.test(email.value)) { setEmailError(true); setEmailErrorMessage("Please enter a valid email address."); isValid = false; } else { setEmailError(false); setEmailErrorMessage(""); } if (!password.value || password.value.length < 6) { setPasswordError(true); setPasswordErrorMessage("Password must be at least 6 characters long."); isValid = false; } else { setPasswordError(false); setPasswordErrorMessage(""); } return isValid; }; return (
Sign in Email Password
); }; export default Login;