From a60dc4effa81a160ac5daef01c9f6a24eb6697ce Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Thu, 27 Apr 2023 19:50:09 -0400 Subject: [PATCH] fix: redirect auth routes to /app if authenticated --- src/App.tsx | 13 ++++++++++--- .../{ => guards}/AuthenticationGuard.tsx | 2 +- src/components/guards/UnauthenticatedGuard.tsx | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) rename src/components/{ => guards}/AuthenticationGuard.tsx (85%) create mode 100644 src/components/guards/UnauthenticatedGuard.tsx diff --git a/src/App.tsx b/src/App.tsx index 10ec409..b712a72 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,13 +1,14 @@ import { observer } from "mobx-react-lite"; import React from "react"; import { Route, Routes } from "react-router-dom"; -import { AuthenticationGuard } from "./components/AuthenticationGuard"; +import { AuthenticationGuard } from "./components/guards/AuthenticationGuard"; import LoadingPage from "./pages/LoadingPage"; import LoginPage from "./pages/LoginPage"; import NotFoundPage from "./pages/NotFound"; import RegistrationPage from "./pages/RegistrationPage"; import { reaction } from "mobx"; +import { UnauthenticatedGuard } from "./components/guards/UnauthenticatedGuard"; import RootPage from "./pages/RootPage"; import { useAppStore } from "./stores/AppStore"; import { Globals } from "./utils/Globals"; @@ -64,8 +65,14 @@ function App() { path="/app" element={} /> - } /> - } /> + } + /> + } + /> } /> ); diff --git a/src/components/AuthenticationGuard.tsx b/src/components/guards/AuthenticationGuard.tsx similarity index 85% rename from src/components/AuthenticationGuard.tsx rename to src/components/guards/AuthenticationGuard.tsx index 5fd5323..6bbe51d 100644 --- a/src/components/AuthenticationGuard.tsx +++ b/src/components/guards/AuthenticationGuard.tsx @@ -1,5 +1,5 @@ import { Navigate } from "react-router-dom"; -import { useAppStore } from "../stores/AppStore"; +import { useAppStore } from "../../stores/AppStore"; interface Props { component: React.FC; diff --git a/src/components/guards/UnauthenticatedGuard.tsx b/src/components/guards/UnauthenticatedGuard.tsx new file mode 100644 index 0000000..5100985 --- /dev/null +++ b/src/components/guards/UnauthenticatedGuard.tsx @@ -0,0 +1,17 @@ +import { Navigate } from "react-router-dom"; +import { useAppStore } from "../../stores/AppStore"; + +interface Props { + component: React.FC; +} + +export const UnauthenticatedGuard = ({ component }: Props) => { + const app = useAppStore(); + + if (app.token) { + return ; + } + + const Component = component; + return ; +};