1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-25 11:42:30 +01:00

fix: redirect auth routes to /app if authenticated

This commit is contained in:
Puyodead1 2023-04-27 19:50:09 -04:00
parent 7de1568b0f
commit a60dc4effa
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
3 changed files with 28 additions and 4 deletions

View File

@ -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={<AuthenticationGuard component={RootPage} />}
/>
<Route path="/login" element={<LoginPage />} />
<Route path="/register" element={<RegistrationPage />} />
<Route
path="/login"
element={<UnauthenticatedGuard component={LoginPage} />}
/>
<Route
path="/register"
element={<UnauthenticatedGuard component={RegistrationPage} />}
/>
<Route path="*" element={<NotFoundPage />} />
</Routes>
);

View File

@ -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;

View File

@ -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 <Navigate to="/app" replace />;
}
const Component = component;
return <Component />;
};