Update frontend/src/components/Login.jsx
Browse files- frontend/src/components/Login.jsx +100 -35
frontend/src/components/Login.jsx
CHANGED
|
@@ -1,36 +1,101 @@
|
|
| 1 |
-
import React from 'react'
|
| 2 |
-
import '../login.css';
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
export default Login
|
|
|
|
| 1 |
+
import React, { useState } from 'react'
|
| 2 |
+
import '../login.css';
|
| 3 |
+
|
| 4 |
+
const Login = ({toSignin, onLoginSuccess}) => {
|
| 5 |
+
// États pour les champs du formulaire
|
| 6 |
+
const [email, setEmail] = useState('');
|
| 7 |
+
const [password, setPassword] = useState('');
|
| 8 |
+
const [error, setError] = useState('');
|
| 9 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 10 |
+
|
| 11 |
+
// Gérer la soumission du formulaire
|
| 12 |
+
const handleSubmit = async (e) => {
|
| 13 |
+
e.preventDefault();
|
| 14 |
+
setError('');
|
| 15 |
+
setIsLoading(true);
|
| 16 |
+
|
| 17 |
+
try {
|
| 18 |
+
const response = await fetch('/api/login', {
|
| 19 |
+
method: 'POST',
|
| 20 |
+
headers: {
|
| 21 |
+
'Content-Type': 'application/json',
|
| 22 |
+
},
|
| 23 |
+
credentials: 'include', // Important pour accepter les cookies!
|
| 24 |
+
body: JSON.stringify({
|
| 25 |
+
email,
|
| 26 |
+
password
|
| 27 |
+
})
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
const data = await response.json();
|
| 31 |
+
|
| 32 |
+
if (response.ok) {
|
| 33 |
+
// Stockage du nom d'utilisateur dans localStorage
|
| 34 |
+
localStorage.setItem('userName', data.username);
|
| 35 |
+
localStorage.setItem('userId', data.user_id);
|
| 36 |
+
|
| 37 |
+
// Informer le composant parent que la connexion est réussie
|
| 38 |
+
if (onLoginSuccess) {
|
| 39 |
+
onLoginSuccess();
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
} else {
|
| 43 |
+
setError(data.detail || 'Identifiants incorrects');
|
| 44 |
+
}
|
| 45 |
+
} catch (err) {
|
| 46 |
+
setError('Erreur de connexion au serveur');
|
| 47 |
+
console.error(err);
|
| 48 |
+
} finally {
|
| 49 |
+
setIsLoading(false);
|
| 50 |
+
}
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
return (
|
| 54 |
+
<div className='login'>
|
| 55 |
+
<h1 className='title-1'>Te voilà de retour sur Medic.ial !</h1>
|
| 56 |
+
<div className="container">
|
| 57 |
+
<div className='container-form'>
|
| 58 |
+
<form onSubmit={handleSubmit} className='form'>
|
| 59 |
+
<h3>CONNEXION</h3>
|
| 60 |
+
|
| 61 |
+
{/* Afficher les erreurs */}
|
| 62 |
+
{error && <div className="error-message">{error}</div>}
|
| 63 |
+
|
| 64 |
+
<div className='form-container-input'>
|
| 65 |
+
<p className='form-title'>Adresse email</p>
|
| 66 |
+
<input
|
| 67 |
+
type='email'
|
| 68 |
+
className='form-input'
|
| 69 |
+
value={email}
|
| 70 |
+
onChange={(e) => setEmail(e.target.value)}
|
| 71 |
+
required
|
| 72 |
+
/>
|
| 73 |
+
</div>
|
| 74 |
+
<div className='form-container-input'>
|
| 75 |
+
<p className='form-title'>Mot de passe</p>
|
| 76 |
+
<input
|
| 77 |
+
type='password'
|
| 78 |
+
className='form-input'
|
| 79 |
+
value={password}
|
| 80 |
+
onChange={(e) => setPassword(e.target.value)}
|
| 81 |
+
required
|
| 82 |
+
/>
|
| 83 |
+
</div>
|
| 84 |
+
<div className='form-container-submit'>
|
| 85 |
+
<button
|
| 86 |
+
type="submit"
|
| 87 |
+
disabled={isLoading}
|
| 88 |
+
>
|
| 89 |
+
{isLoading ? 'Connexion...' : 'Connexion'}
|
| 90 |
+
</button>
|
| 91 |
+
<p>Mot de passe oublié?</p>
|
| 92 |
+
</div>
|
| 93 |
+
</form>
|
| 94 |
+
</div>
|
| 95 |
+
</div>
|
| 96 |
+
<p className='title-2'>Pas encore de compte ? <span onClick={toSignin}>S'inscrire</span></p>
|
| 97 |
+
</div>
|
| 98 |
+
)
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
export default Login
|