From 83d1a1c558293e1b14c9a5847628e7661f749c66 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Mon, 21 Oct 2024 14:54:11 +0700 Subject: initial commit --- app/login/page.tsx | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 app/login/page.tsx (limited to 'app/login') diff --git a/app/login/page.tsx b/app/login/page.tsx new file mode 100644 index 0000000..8df5b2c --- /dev/null +++ b/app/login/page.tsx @@ -0,0 +1,174 @@ +"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"); + + console.log('ini user', email, 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; + } + console.log('ini akhir',res); + }); + } catch (error) { + 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; -- cgit v1.2.3