Ajout de la connexion utilisateur
This commit is contained in:
1 parent
05eed05947
commit
387b2c32cf
4 files changed
+86
-5
No files matched your search
+22
-5
@@ -1,22 +1,39 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import login from "../utils/login.js";
|
||||||
|
import getUser from "../utils/getUser.js";
|
||||||
|
|
||||||
function LoginForm({ className }) {
|
function LoginForm({ className }) {
|
||||||
|
|
||||||
|
const [email, setEmail] = useState('');
|
||||||
|
const [password, setPassword] = useState('');
|
||||||
|
|
||||||
|
async function handleSubmit(e) {
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
await login(email, password);
|
||||||
|
getUser();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form className={className} method="POST" action="#">
|
<div className={className}>
|
||||||
<h1>Connexion</h1>
|
<h1>Connexion</h1>
|
||||||
|
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<br/>
|
<br/>
|
||||||
<label htmlFor="email" className="floating-label">Adresse e-mail</label>
|
<label htmlFor="email" className="floating-label">Adresse e-mail</label>
|
||||||
<input type="email"id="email"required/>
|
<input type="email" id="email" value={email} onChange={e => setEmail(e.target.value)} required/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label htmlFor="password" className="floating-label">Mot de passe</label>
|
<label htmlFor="password" className="floating-label">Mot de passe</label>
|
||||||
<input type="password"id="password"required/>
|
<input type="password" id="password" value={password} onChange={e => setPassword(e.target.value)} required/>
|
||||||
<br/>
|
<br/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit" className="login-button">Se connecter</button>
|
<button className="login-button" onClick={e => handleSubmit(e)}>Se connecter</button>
|
||||||
</form>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
export default LoginForm;
|
export default LoginForm;
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
export default async function getUser() {
|
||||||
|
await fetch('http://localhost:8000/api/me', {
|
||||||
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => console.log('Utilisateur connecté :', data))
|
||||||
|
.catch(err => console.error('Erreur :', err));
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
export default async function getXSRFToken() {
|
||||||
|
const response = await fetch('http://localhost:8000/sanctum/csrf-cookie', {
|
||||||
|
method: 'GET',
|
||||||
|
credentials: 'include'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok && response.status !== 204) {
|
||||||
|
throw new Error(`Error : ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = 'XSRF-TOKEN';
|
||||||
|
const value = `; ${document.cookie}`;
|
||||||
|
const parts = value.split(`; ${name}=`);
|
||||||
|
if (parts.length === 2) {
|
||||||
|
return decodeURIComponent(parts.pop().split(';').shift());
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Error: Invalid XSRF-TOKEN');
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import getXSRFToken from "./getXSRF.js";
|
||||||
|
|
||||||
|
export default async function login(email, password) {
|
||||||
|
const csrfToken = await getXSRFToken();
|
||||||
|
|
||||||
|
|
||||||
|
await fetch('http://localhost:8000/api/login', {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-XSRF-TOKEN': csrfToken,
|
||||||
|
'Accept': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
email: email,
|
||||||
|
password: password
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
console.log('Code HTTP :', response.status);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Erreur HTTP ${response.status}`);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
console.log('Connexion réussie ! Données utilisateur :', data);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Erreur lors de la connexion :', error.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user