From d737280f1b33e0596d1342e20d6cf3544a97dd47 Mon Sep 17 00:00:00 2001 From: Baptiste Marcel Date: Tue, 14 Nov 2023 11:30:03 +0100 Subject: [PATCH] =?UTF-8?q?Gestion=20et=20affichage=20des=20erreurs=20+=20?= =?UTF-8?q?redirection=20apr=20connexion=20r=C3=A9ussi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cryptide_project/src/Pages/LoginForm.tsx | 124 +++++++++++------- cryptide_project/src/Pages/SignUpForm.tsx | 4 +- cryptide_project/src/services/AuthService.tsx | 36 ++--- .../src/services/VerificationService.tsx | 12 ++ 4 files changed, 115 insertions(+), 61 deletions(-) diff --git a/cryptide_project/src/Pages/LoginForm.tsx b/cryptide_project/src/Pages/LoginForm.tsx index a8b3581..fae874f 100644 --- a/cryptide_project/src/Pages/LoginForm.tsx +++ b/cryptide_project/src/Pages/LoginForm.tsx @@ -1,53 +1,76 @@ -import React, { Component } from 'react'; +import React, { useState, useEffect } from 'react'; import { AiOutlineSend } from 'react-icons/ai'; +import { useNavigate } from 'react-router-dom'; import AuthService from '../services/AuthService'; import '../Style/Global.css'; -export default class Login extends Component { - handleSubmit = async (event: any) => { +const SignIn = () => { + const navigate = useNavigate(); + + const [error, setError] = useState(null); + const [showConfirmation, setShowConfirmation] = useState(false); + + const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); - try{ + + try { const data = { - pseudo: event.target.pseudo.value, - password: event.target.password.value, - remember: event.target.remember.value + pseudo: (event.target as any).pseudo.value, + password: (event.target as any).password.value, + remember: (event.target as any).remember.checked }; - const result = await AuthService.signIn(data); - console.log(result); - alert('Connexion réussie !'); - } - catch(error){ - console.error(error); + const validation = await AuthService.validateSignIn(data); + + if (!validation.valid) { + setError(validation.error); + } else { + setError(null); + + const result = await AuthService.signIn(data); + // console.log(result); + + setShowConfirmation(true); + setTimeout(() => { + navigate('/play'); // 3 secondes avant de rediriger vers la page de connexion + }, 3000); + } + } catch (error: any) { + setError(error.message); } }; - render() { - return ( -
-
-
-

Log In

-
- - { + return () => { + setShowConfirmation(false); + }; + }, []); + + return ( +
+ +
+

Log In

+
+ + -
-
- - +
+
+ + -
-
-
+ /> +
+
+
Se souvenir de moi -
-
-
+
+ -
-

- Mot de passe oublié ? -

- -
- ) - } -} + +
+

+ Mot de passe oublié ? +

+ + + {error && ( +
+ {error} +
+ )} + + {showConfirmation && ( +
+ Connexion réussie ! Vous serez redirigé vers votre profil dans 3 secondes. +
+ )} +
+ ); +}; + +export default SignIn; diff --git a/cryptide_project/src/Pages/SignUpForm.tsx b/cryptide_project/src/Pages/SignUpForm.tsx index 2ec2e77..876f803 100644 --- a/cryptide_project/src/Pages/SignUpForm.tsx +++ b/cryptide_project/src/Pages/SignUpForm.tsx @@ -1,11 +1,11 @@ import React, { useState, useEffect } from 'react'; import { AiOutlineSend } from 'react-icons/ai'; -import { useNavigate } from 'react-router-dom'; // Use useNavigate instead of useHistory +import { useNavigate } from 'react-router-dom'; import AuthService from '../services/AuthService'; import '../Style/Global.css'; const SignUp = () => { - const navigate = useNavigate(); // Use useNavigate instead of useHistory + const navigate = useNavigate(); const [error, setError] = useState(null); const [showConfirmation, setShowConfirmation] = useState(false); diff --git a/cryptide_project/src/services/AuthService.tsx b/cryptide_project/src/services/AuthService.tsx index 0cd3436..5d275e9 100644 --- a/cryptide_project/src/services/AuthService.tsx +++ b/cryptide_project/src/services/AuthService.tsx @@ -7,6 +7,10 @@ class AuthService{ return VerificationService.validateSignUpData(data); } + static async validateSignIn(data: any): Promise<{valid: boolean, error: string}> { + return VerificationService.validateSignInData(data); + } + static async signUp(data: any) { try { const response = await fetch('http://localhost:3000/auth/signup', { @@ -32,21 +36,23 @@ class AuthService{ static async signIn(data: any) { try { - const response = await fetch('http://localhost:3000/auth/signin', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(data), - }); - - if (response.ok) { - const result = await response.json(); - return result; - } else { - throw new Error('Échec de la connexion.'); - } - } catch (error) { + const response = await fetch('http://localhost:3000/auth/signin', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }); + + if (response.ok) { + const result = await response.json(); + return result; + } else { + const errorResponse = await response.json(); + throw new Error(errorResponse.error); + } + } + catch (error) { console.error(error); throw error; } diff --git a/cryptide_project/src/services/VerificationService.tsx b/cryptide_project/src/services/VerificationService.tsx index 7b5393a..bef4dcb 100644 --- a/cryptide_project/src/services/VerificationService.tsx +++ b/cryptide_project/src/services/VerificationService.tsx @@ -14,6 +14,18 @@ class ValidationService { return {valid: true, error: ''}; } + + public static validateSignInData(data: any): {valid: boolean, error: string} { + if(!data.pseudo || !data.password) { + return {valid: false, error: 'Veuillez remplir tous les champs.'}; + } + + if(data.password.length < 8) { + return {valid: false, error: 'Le mot de passe doit contenir au moins 8 caractères.'}; + } + + return {valid: true, error: ''}; + } } export default ValidationService; \ No newline at end of file