Login actions

This commit is contained in:
2024-09-28 16:54:56 +02:00
parent 90ddfd1da5
commit ce5019ca38
5 changed files with 200 additions and 35 deletions
+64 -2
View File
@@ -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,
-11
View File
@@ -1,11 +0,0 @@
import { useRef } from 'react';
export const useData = () => {
const emailRef = useRef(null);
const passwordRef = useRef(null);
return {
emailRef,
passwordRef,
};
};