fix redir depending on auth state
This commit is contained in:
parent
e3720181f8
commit
279a866812
|
|
@ -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 { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
import Login from './routes/login';
|
import Login from './routes/login';
|
||||||
import Home from './routes/home';
|
import Home from './routes/home';
|
||||||
|
import { checkAuth } from './controllers/Auth';
|
||||||
|
|
||||||
// Create a client
|
// Create a client
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
@ -11,8 +12,8 @@ export default () => {
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/login" element={<Login />} />
|
<Route path="/login" element={checkAuth() ? <Navigate to="/home" /> : <Login />} />
|
||||||
<Route path="/home" element={<Home />} />
|
<Route path="/home" element={!checkAuth() ? <Navigate to="/login" /> : <Home />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import { useCookies } from "react-cookie";
|
import { useCookies } from "react-cookie";
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
|
|
||||||
const checkAuth = () => {
|
const checkAuth = () => {
|
||||||
const [cookie, setCookie] = useCookies(['token']);
|
const [cookie, setCookie] = useCookies(['token']);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
import AppHeader from "@components/AppHeader";
|
import AppHeader from "@components/AppHeader";
|
||||||
import { Navigate } from "react-router-dom";
|
|
||||||
import { checkAuth } from "../controllers/Auth";
|
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
if (!checkAuth()) {
|
|
||||||
return <Navigate to="/login" />;
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AppHeader />
|
<AppHeader />
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,8 @@ import { useQuery } from '@tanstack/react-query';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useCookies } from 'react-cookie';
|
import { useCookies } from 'react-cookie';
|
||||||
import type { Token } from '../types';
|
import type { Token } from '../types';
|
||||||
import { checkAuth } from '../controllers/Auth';
|
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
if (checkAuth()) {
|
|
||||||
return <Navigate to="/home" />;
|
|
||||||
}
|
|
||||||
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('');
|
||||||
|
|
@ -30,7 +26,7 @@ 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) });
|
||||||
return <Navigate to="/home" />;
|
return <Navigate to="/" />;
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue