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:
parent
7de1568b0f
commit
a60dc4effa
13
src/App.tsx
13
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={<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>
|
||||
);
|
||||
|
@ -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;
|
17
src/components/guards/UnauthenticatedGuard.tsx
Normal file
17
src/components/guards/UnauthenticatedGuard.tsx
Normal 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 />;
|
||||
};
|
Loading…
Reference in New Issue
Block a user