From 279a866812545f0b06c77a9d97f1df7cf7001767 Mon Sep 17 00:00:00 2001 From: Guillaume Dorce Date: Fri, 9 Sep 2022 15:48:32 +0200 Subject: [PATCH] fix redir depending on auth state --- client/src/App.tsx | 7 ++++--- client/src/controllers/Auth.ts | 1 - client/src/routes/home.tsx | 5 ----- client/src/routes/login.tsx | 6 +----- 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index f24698d..93c6bc5 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,7 +1,8 @@ -import { BrowserRouter, useNavigate, Outlet, Route, Routes } from 'react-router-dom'; +import { BrowserRouter, Navigate, Outlet, Route, Routes } from 'react-router-dom'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import Login from './routes/login'; import Home from './routes/home'; +import { checkAuth } from './controllers/Auth'; // Create a client const queryClient = new QueryClient(); @@ -11,8 +12,8 @@ export default () => { - } /> - } /> + : } /> + : } /> diff --git a/client/src/controllers/Auth.ts b/client/src/controllers/Auth.ts index 0bfea26..e15ac9b 100644 --- a/client/src/controllers/Auth.ts +++ b/client/src/controllers/Auth.ts @@ -1,5 +1,4 @@ import { useCookies } from "react-cookie"; -import { useNavigate } from "react-router-dom"; const checkAuth = () => { const [cookie, setCookie] = useCookies(['token']); diff --git a/client/src/routes/home.tsx b/client/src/routes/home.tsx index 673e602..3507e7b 100644 --- a/client/src/routes/home.tsx +++ b/client/src/routes/home.tsx @@ -1,11 +1,6 @@ import AppHeader from "@components/AppHeader"; -import { Navigate } from "react-router-dom"; -import { checkAuth } from "../controllers/Auth"; const Home = () => { - if (!checkAuth()) { - return ; - } return ( <> diff --git a/client/src/routes/login.tsx b/client/src/routes/login.tsx index 815081b..e1b96ad 100644 --- a/client/src/routes/login.tsx +++ b/client/src/routes/login.tsx @@ -4,12 +4,8 @@ import { useQuery } from '@tanstack/react-query'; import { useState } from 'react'; import { useCookies } from 'react-cookie'; import type { Token } from '../types'; -import { checkAuth } from '../controllers/Auth'; const Login = () => { - if (checkAuth()) { - return ; - } const [cookie, setCookie] = useCookies(['token']); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); @@ -30,7 +26,7 @@ const Login = () => { { onSuccess: (data: Token) => { setCookie('token', data.token, { path: '/', expires: new Date(data.expiresAt) }); - return ; + return ; }, onError: (error) => { console.error(error);