From c42cbf6b76cb0bf353cd52279f02e78dad5b4efb Mon Sep 17 00:00:00 2001 From: Guillaume Dorce Date: Fri, 16 Sep 2022 19:20:54 +0200 Subject: [PATCH] redirect based on cookie --- client/src/App.tsx | 34 +++++++++++++++++++---------- client/src/components/AppHeader.tsx | 6 ++--- client/src/routes/login.tsx | 9 +++----- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 2f8d499..bf4d0d3 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -4,28 +4,38 @@ import Login from './routes/login'; import Home from './routes/home'; import { CookiesProvider, useCookies } from 'react-cookie'; import Signup from './routes/signup'; -import { useEffect, useState } from 'react'; // Create a client const queryClient = new QueryClient(); -export default () => { - const [auth, setAuth] = useState(false); - const [cookies, setCookie, removeCookie] = useCookies(['token']); - useEffect(() => { - if (cookies.token) { - setAuth(true); - } - }, [cookies.token]); +const Auth = ({route}: {route: string}) => { + const [cookies] = useCookies(['token']); + if (cookies.token && route === 'login') { + return ; + } else if (!cookies.token && route === 'home') { + return ; + } else if (cookies.token && route === 'signup') { + return ; + } else if (cookies.token && route === 'home') { + return ; + } else if (!cookies.token && route === 'login') { + return ; + } else if (!cookies.token && route === 'signup') { + return ; + } + return ; +}; + +export default () => { return ( - : } /> - : } /> - : } /> + } /> + } /> + } /> diff --git a/client/src/components/AppHeader.tsx b/client/src/components/AppHeader.tsx index 58d7ed6..7cf55cb 100644 --- a/client/src/components/AppHeader.tsx +++ b/client/src/components/AppHeader.tsx @@ -27,10 +27,8 @@ const AppHeader = () => { }); const [cookies, setCookie, removeCookie] = useCookies(['token']); - const logOut = async () => { - console.log(cookies); + const logOut = () => { removeCookie('token'); - window.location.href = '/login'; }; return ( @@ -51,7 +49,7 @@ const AppHeader = () => { 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 + Deconnexion diff --git a/client/src/routes/login.tsx b/client/src/routes/login.tsx index 7af3040..67dd2ad 100644 --- a/client/src/routes/login.tsx +++ b/client/src/routes/login.tsx @@ -1,15 +1,14 @@ -import { Link, Navigate, useNavigate } from 'react-router-dom'; +import { Link } from 'react-router-dom'; import logo from '@assets/images/logo.svg'; import { useQuery } from '@tanstack/react-query'; -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import { useCookies } from 'react-cookie'; import type { Token } from '../types'; -import { useAuth } from '../controllers/Auth'; const Login = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); - const [cookies, setCookie, removeCookie] = useCookies(['token']); + const [cookies, setCookie] = useCookies(['token']); const { refetch } = useQuery( ['login'], @@ -26,9 +25,7 @@ const Login = () => { }, { onSuccess: (data: Token) => { - console.log(data); setCookie('token', data.token, { path: '/', expires: new Date(data.expiresAt) }); - window.location.href = '/home'; }, onError: (error) => { console.error(error);