diff --git a/app/auth/login/hooks/useActions.js b/app/auth/login/hooks/useActions.js index 6438d87..f4139ed 100644 --- a/app/auth/login/hooks/useActions.js +++ b/app/auth/login/hooks/useActions.js @@ -1,5 +1,67 @@ -export const useActions = ({ emailRef, passwordRef }) => { - const handleSubmit = () => {}; +import { useRouter } from 'next/navigation'; + +export const useActions = ({ setError, setIsLoading }) => { + const router = useRouter(); + + const handleSubmit = async (ev) => { + ev.preventDefault(); + + const formData = new FormData(ev.currentTarget); + + const username = formData.get('username'); + const password = formData.get('password'); + + if (!username || !password) { + return; + } + + setIsLoading(true); + try { + const request = await fetch( + 'https://lovemap-backend.vercel.app/auth/login', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + username, + password, + }), + } + ); + + const response = await request.json(); + + if (request.status == 404) { + setError('Utilisateur inconnu'); + setIsLoading(false); + return; + } + if (request.status == 403) { + setError('Mauvais mot de passe'); + setIsLoading(false); + return; + } + if (request.status == 500) { + console.log('request.status: ', request.status); + setError('Erreur interne. Veuillez réessayer'); + setIsLoading(false); + return; + } + + const { token } = response; + + localStorage.setItem('token', token); + + router.push('/'); + } catch (error) { + console.error(error); + setError('Erreur interne. Veuillez réessayer'); + } + + setIsLoading(false); + }; return { handleSubmit, diff --git a/app/auth/login/hooks/useData.js b/app/auth/login/hooks/useData.js deleted file mode 100644 index ceb66d0..0000000 --- a/app/auth/login/hooks/useData.js +++ /dev/null @@ -1,11 +0,0 @@ -import { useRef } from 'react'; - -export const useData = () => { - const emailRef = useRef(null); - const passwordRef = useRef(null); - - return { - emailRef, - passwordRef, - }; -}; diff --git a/app/auth/login/page.jsx b/app/auth/login/page.jsx index 2ff15ed..dd76ab2 100644 --- a/app/auth/login/page.jsx +++ b/app/auth/login/page.jsx @@ -4,23 +4,27 @@ //! Imports // --------------------------------------------------------------------------------------------------------------------- +// -------------------------------------------------- React & Next ----------------------------------------------------- +import { useState } from 'react'; +// --------------------------------------------------------------------------------------------------------------------- + // --------------------------------------------------- Components ------------------------------------------------------ import Image from 'next/image'; -import Link from 'next/link'; // --------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------- Hooks & Utils ---------------------------------------------------- -import { useData } from './hooks/useData'; import { useActions } from './hooks/useActions'; // --------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------- Assets & Styles --------------------------------------------------- +import Loader from '@/assets/Loader'; import '../styles.scss'; // --------------------------------------------------------------------------------------------------------------------- const Login = () => { - const { emailRef, passwordRef } = useData(); - const { handleSubmit } = useActions({ emailRef, passwordRef }); + const [error, setError] = useState(null); + const [isLoading, setIsLoading] = useState(false); + const { handleSubmit } = useActions({ setError, setIsLoading }); return (