use window location to reload

This commit is contained in:
Guillaume Dorce 2022-09-09 17:42:24 +02:00
parent dc3fd4e474
commit 9b3381bfe6
3 changed files with 17 additions and 12 deletions

View File

@ -1,7 +1,8 @@
import logo from '@assets/images/logo.svg'; import logo from '@assets/images/logo.svg';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { Cookies, withCookies } from 'react-cookie'; import { Cookies, withCookies } from 'react-cookie';
import { useNavigate } from 'react-router-dom'; import { Navigate, useNavigate } from 'react-router-dom';
const getMeInfo = async () => { const getMeInfo = async () => {
const token = new Cookies().get('token'); const token = new Cookies().get('token');
@ -16,8 +17,6 @@ const getMeInfo = async () => {
return response.json(); return response.json();
}; };
const AppHeader = () => { const AppHeader = () => {
const meInfo = useQuery(['me'], getMeInfo, { const meInfo = useQuery(['me'], getMeInfo, {
onSuccess: (data) => { onSuccess: (data) => {
@ -28,6 +27,11 @@ const AppHeader = () => {
}, },
}); });
const logOut = async () => {
new Cookies().remove('token');
window.location.reload();
};
return ( return (
<div className="min-h-80 flex items-center justify-between bg-grey-dark p-2"> <div className="min-h-80 flex items-center justify-between bg-grey-dark p-2">
<div className="app-header__logo"> <div className="app-header__logo">
@ -43,7 +47,9 @@ const AppHeader = () => {
{meInfo.isLoading ? '' : meInfo.data ? meInfo.data.firstName + ' ' + meInfo.data.lastName : ''} {meInfo.isLoading ? '' : meInfo.data ? meInfo.data.firstName + ' ' + meInfo.data.lastName : ''}
</span> </span>
</div> </div>
<button className="group relative flex w-auto justify-center rounded-md border border-red bg-red py-2 px-2 text-sm font-medium text-white hover:bg-white hover:text-red focus:outline-none focus:ring-2 focus:ring-red focus:ring-offset-2" <button
className="group relative flex w-auto justify-center rounded-md border border-red bg-red py-2 px-2 text-sm font-medium text-white hover:bg-white hover:text-red focus:outline-none focus:ring-2 focus:ring-red focus:ring-offset-2"
onClick={() => logOut()}
> >
Deonnexion Deonnexion
</button> </button>

View File

@ -1,9 +1,8 @@
import { useCookies } from "react-cookie"; import { Cookies } from "react-cookie";
const checkAuth = () => { const checkAuth = () => {
const [cookie, setCookie] = useCookies(['token']); const token = new Cookies().get("token");
if (token && token !== '') {
if (cookie.token && cookie.token !== '') {
return true; return true;
} }
return false; return false;

View File

@ -1,4 +1,4 @@
import { Link, Navigate } from 'react-router-dom'; import { Link, Navigate, useNavigate } from 'react-router-dom';
import logo from '@assets/images/logo.svg'; import logo from '@assets/images/logo.svg';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { useState } from 'react'; import { useState } from 'react';
@ -9,7 +9,7 @@ const Login = () => {
const [cookie, setCookie] = useCookies(['token']); const [cookie, setCookie] = useCookies(['token']);
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const { refetch } = useQuery( const { refetch } = useQuery(
['login'], ['login'],
async () => { async () => {
@ -25,8 +25,8 @@ const Login = () => {
}, },
{ {
onSuccess: (data: Token) => { onSuccess: (data: Token) => {
setCookie('token', data.token, { path: '/', expires: new Date(data.expiresAt) }); setCookie('token', data.token, { path: '/', expires: new Date(data.expiresAt), sameSite: 'strict' });
return <Navigate to="/" />; window.location.reload();
}, },
onError: (error) => { onError: (error) => {
console.error(error); console.error(error);