diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-28 09:05:00 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-28 09:05:00 +0700 |
| commit | c82110f7d3a2f85de99045fde7b579e369f15b2c (patch) | |
| tree | deae1b959583eff4fa283800efe6daaff9fe21e9 /src/lib/auth/hooks/useLogin.js | |
| parent | cf6da809621b4ebe8c9acedb035b689e7e1b60b1 (diff) | |
Update authentication feature
Diffstat (limited to 'src/lib/auth/hooks/useLogin.js')
| -rw-r--r-- | src/lib/auth/hooks/useLogin.js | 101 |
1 files changed, 52 insertions, 49 deletions
diff --git a/src/lib/auth/hooks/useLogin.js b/src/lib/auth/hooks/useLogin.js index 17731a86..dc9580ea 100644 --- a/src/lib/auth/hooks/useLogin.js +++ b/src/lib/auth/hooks/useLogin.js @@ -1,80 +1,83 @@ -import Link from '@/core/components/elements/Link/Link' -import { setAuth } from '@/core/utils/auth' -import { useRouter } from 'next/router' -import { useRef, useState } from 'react' -import loginApi from '../api/loginApi' -import odooApi from '@/core/api/odooApi' -import { setCookie } from 'cookies-next' -import { useSession } from 'next-auth/react' +import Link from '@/core/components/elements/Link/Link'; +import { setAuth } from '@/core/utils/auth'; +import { useRouter } from 'next/router'; +import { useRef, useState } from 'react'; +import loginApi from '../api/loginApi'; +import odooApi from '@/core/api/odooApi'; +import { setCookie } from 'cookies-next'; +import { useSession } from 'next-auth/react'; const useLogin = () => { - const router = useRouter() - const [isLoading, setIsLoading] = useState(false) - const [alert, setAlert] = useState(null) - const [isValid, setIsValid] = useState(false) + const router = useRouter(); + const [isLoading, setIsLoading] = useState(false); + const [alert, setAlert] = useState(null); + const [isValid, setIsValid] = useState(false); - const emailRef = useRef(null) - const passwordRef = useRef(null) + const emailRef = useRef(null); + const passwordRef = useRef(null); const inputVal = () => ({ email: emailRef.current.value, - password: passwordRef.current.value - }) + password: passwordRef.current.value, + }); const handleChangeInput = () => { - const { email, password } = inputVal() - const isValidInput = email && password - setIsValid(isValidInput) - } + const { email, password } = inputVal(); + const isValidInput = email && password; + setIsValid(isValidInput); + }; const handleSubmit = async (e) => { - e.preventDefault() - setAlert(null) - setIsLoading(true) - const { email, password } = inputVal() - const login = await loginApi({ email, password }) - setIsLoading(false) + e.preventDefault(); + setAlert(null); + setIsLoading(true); + const { email, password } = inputVal(); + const login = await loginApi({ email, password }); + setIsLoading(false); if (login.isAuth) { - setAuth(login.user) - router.push(router.query?.next || '/') - return + setAuth(login.user); + router.push(router.query?.next || '/'); + return; } switch (login.reason) { case 'NOT_FOUND': setAlert({ children: 'Email atau password tidak cocok', - type: 'info' - }) - break + type: 'info', + }); + break; case 'NOT_ACTIVE': setAlert({ children: ( <> Email belum diaktivasi, - <Link className='text-gray-900' href={`/activate?email=${email}`}> + <Link + className='text-gray-900' + href={`?activation=email&email=${email}`} + > aktivasi sekarang </Link> </> ), - type: 'info' - }) - break + type: 'info', + }); + break; } - } + }; const handleGoogleSubmit = async (session) => { const params = { - access_token: session.accessToken - } - const data = await odooApi('POST', '/api/v1/user/validate-sso', params) + access_token: session.accessToken, + }; + const data = await odooApi('POST', '/api/v1/user/validate-sso', params); if (data.isAuth) { - session.odooUser = data.user - setCookie('auth', JSON.stringify(session?.odooUser)) - router.push(decodeURIComponent(router?.query?.next) ?? '/') - return + session.odooUser = data.user; + setCookie('auth', JSON.stringify(session?.odooUser)); + router.push(decodeURIComponent(router?.query?.next) ?? '/'); + return; } - } + }; return { handleChangeInput, @@ -84,8 +87,8 @@ const useLogin = () => { alert, emailRef, passwordRef, - handleGoogleSubmit - } -} + handleGoogleSubmit, + }; +}; -export default useLogin +export default useLogin; |
