From e33a330786ffbfcd774de00dc697c6dff47faf27 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 20 Feb 2023 14:20:44 +0700 Subject: fix --- src/lib/auth/api/loginApi.js | 12 +++++ src/lib/auth/components/Login.jsx | 99 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/lib/auth/api/loginApi.js create mode 100644 src/lib/auth/components/Login.jsx (limited to 'src/lib/auth') diff --git a/src/lib/auth/api/loginApi.js b/src/lib/auth/api/loginApi.js new file mode 100644 index 00000000..4782680c --- /dev/null +++ b/src/lib/auth/api/loginApi.js @@ -0,0 +1,12 @@ +import odooApi from "@/core/api/odooApi" + +const loginApi = async ({email, password}) => { + let result = await odooApi( + 'POST', + '/api/v1/user/login', + {email, password} + ) + return result +} + +export default loginApi \ No newline at end of file diff --git a/src/lib/auth/components/Login.jsx b/src/lib/auth/components/Login.jsx new file mode 100644 index 00000000..e598fe48 --- /dev/null +++ b/src/lib/auth/components/Login.jsx @@ -0,0 +1,99 @@ +import Image from "next/image" +import IndoteknikLogo from "@/images/logo.png" +import Link from "@/core/components/elements/Link/Link" +import { useState } from "react" +import loginApi from "../api/loginApi" +import { useRouter } from "next/router" +import Alert from "@/core/components/elements/Alert/Alert" +import { setAuth } from "@/core/utils/auth" + +const Login = () => { + const router = useRouter() + const [ email, setEmail ] = useState('') + const [ password, setPassword ] = useState('') + const [ isLoading, setIsLoading ] = useState(false) + const [ alert, setAlert ] = useState(null) + + const handleSubmit = async (e) => { + e.preventDefault() + setIsLoading(true) + const login = await loginApi({ email, password }) + setIsLoading(false) + + if (login.isAuth) { + setAuth(login.user) + router.push('/') + return + } + switch (login.reason) { + case 'NOT_FOUND': + setAlert({ + children: 'Email atau password tidak cocok', + type: 'info' + }) + break + case 'NOT_ACTIVE': + setAlert({ + children: ( + <> + Email belum diaktivasi, + aktivasi sekarang + + ), + type: 'info' + }) + break + } + } + + return ( +
+ + Logo Indoteknik + +

Mulai Belanja Sekarang

+

Masuk ke akun kamu untuk belanja

+ + { alert && ( + + { alert.children } + + ) } + +
+
+ + setEmail(e.target.value)} + placeholder="contoh@email.com" + /> +
+
+ + setPassword(e.target.value)} + placeholder="••••••••••••" + /> +
+ +
+

Belum punya akun Indoteknik? Daftar

+
+ ) +} + +export default Login \ No newline at end of file -- cgit v1.2.3