Add logout btn
This commit is contained in:
1 parent
37e4968023
commit
22065fbd16
5 files changed
+48
-7
No files matched your search
@@ -2,6 +2,7 @@ import { useState, useEffect } from "react";
|
|||||||
import loginFunction from "../../utils/login.js";
|
import loginFunction from "../../utils/login.js";
|
||||||
import getUser from "../../utils/getUser.js";
|
import getUser from "../../utils/getUser.js";
|
||||||
import { AuthContext } from "./AuthContext";
|
import { AuthContext } from "./AuthContext";
|
||||||
|
import logoutFunction from "../../utils/auth/logout.js"
|
||||||
|
|
||||||
|
|
||||||
export function AuthProvider({ children }) {
|
export function AuthProvider({ children }) {
|
||||||
@@ -25,7 +26,7 @@ export function AuthProvider({ children }) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
// TODO: Appeller fonction logout
|
await logoutFunction();
|
||||||
setUser(null);
|
setUser(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
padding: 0;
|
padding: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
overflow-x: hidden;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
|
|||||||
@@ -8,9 +8,13 @@ import { useNavigate } from "react-router";
|
|||||||
|
|
||||||
function ProfilePage() {
|
function ProfilePage() {
|
||||||
|
|
||||||
const { user } = useContext(AuthContext);
|
const { user, logout } = useContext(AuthContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const handleLogout = () => {
|
||||||
|
logout();
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
<div className={`${styles.profileboard} glassCard`}>
|
<div className={`${styles.profileboard} glassCard`}>
|
||||||
@@ -23,7 +27,10 @@ function ProfilePage() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.description}>
|
<div className={styles.description}>
|
||||||
<h2> {user.name} {user.lastname} </h2>
|
<div className={styles.headerProfile}>
|
||||||
|
<h2> {user.name} {user.lastname} </h2>
|
||||||
|
<Button variant={"danger"} onClick={handleLogout}> Déconnexion </Button>
|
||||||
|
</div>
|
||||||
<div className={styles.topDescription}>
|
<div className={styles.topDescription}>
|
||||||
<div className={styles.infoBlock}>
|
<div className={styles.infoBlock}>
|
||||||
<p><strong>Role :</strong> {user.role}</p>
|
<p><strong>Role :</strong> {user.role}</p>
|
||||||
|
|||||||
@@ -66,7 +66,7 @@
|
|||||||
}
|
}
|
||||||
.description h2{
|
.description h2{
|
||||||
font-size:30px;
|
font-size:30px;
|
||||||
margin-bottom: 2%;
|
|
||||||
}
|
}
|
||||||
.profilePicture{
|
.profilePicture{
|
||||||
width:90%;
|
width:90%;
|
||||||
@@ -105,7 +105,6 @@
|
|||||||
}
|
}
|
||||||
.description h2{
|
.description h2{
|
||||||
font-size:30px;
|
font-size:30px;
|
||||||
margin-bottom: 2%;
|
|
||||||
}
|
}
|
||||||
.profilePicture{
|
.profilePicture{
|
||||||
width:90%;
|
width:90%;
|
||||||
@@ -122,6 +121,14 @@
|
|||||||
width:50px;
|
width:50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.headerProfile {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
/* Desktop (1024px et plus) */
|
/* Desktop (1024px et plus) */
|
||||||
@media screen and (min-width: 1024px) {
|
@media screen and (min-width: 1024px) {
|
||||||
.container {
|
.container {
|
||||||
@@ -151,6 +158,5 @@
|
|||||||
}
|
}
|
||||||
.description h2{
|
.description h2{
|
||||||
font-size:30px;
|
font-size:30px;
|
||||||
margin-bottom: 2%;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import getXSRFToken from "../getXSRF.js";
|
||||||
|
|
||||||
|
export default async function logout() {
|
||||||
|
|
||||||
|
const csrfToken = await getXSRFToken();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`http://${import.meta.env.VITE_API_URL}/api/users/logout`, {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'X-XSRF-TOKEN': csrfToken
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Code HTTP :', response.status);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Erreur HTTP ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: response.status };
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user