routing fix

This commit is contained in:
Leon
2022-02-02 15:52:09 +02:00
parent 2a11cb9dc5
commit d906806f6a
6 changed files with 53 additions and 70 deletions

View File

@@ -1,87 +1,27 @@
import React, {useCallback, useEffect, useState} from "react"; import React, { useState} from "react";
import {Route, Routes, useLocation, useNavigate, useParams, useSearchParams} from "react-router-dom"; import {Route, Routes} from "react-router-dom";
import {RouterRoutes} from "../helpers/routes"; import {RouterRoutes} from "../helpers/routes";
import {useRecoilState} from "recoil";
import entPageAtom, {Page} from "../recoil/entPage";
import {toast} from "react-toastify";
import AuthPageBase from "./Pages/AuthPage/AuthPageBase"; import AuthPageBase from "./Pages/AuthPage/AuthPageBase";
import InstallPage from "./Pages/AuthPage/InstallPage"; import InstallPage from "./Pages/AuthPage/InstallPage";
import LoginPage from "./Pages/AuthPage/LoginPage"; import LoginPage from "./Pages/AuthPage/LoginPage";
import LoadingOverlay from "./LoadingOverlay";
import SystemViewer from "./Pages/SystemViewer/SystemViewer"; import SystemViewer from "./Pages/SystemViewer/SystemViewer";
import Api from "../helpers/api";
import {TrafficPage} from "./Pages/TrafficPage/TrafficPage"; import {TrafficPage} from "./Pages/TrafficPage/TrafficPage";
import SettingsPage from "./Pages/SettingsPage/SettingsPage"; import SettingsPage from "./Pages/SettingsPage/SettingsPage";
import { AuthenticatedRoute } from "../helpers/AuthenticatedRoute";
const api = Api.getInstance();
const AppSwitchRoutes = () => { const AppSwitchRoutes = () => {
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(true);
const [entPage, setEntPage] = useRecoilState(entPageAtom);
const [isFirstLogin, setIsFirstLogin] = useState(false); const [isFirstLogin, setIsFirstLogin] = useState(false);
let {inviteToken} = useParams()
const location = useLocation();
const determinePage = useCallback(async () => {
try {
const isInstallNeeded = await api.isInstallNeeded();
if (isInstallNeeded) {
setEntPage(Page.Setup);
} else {
const isAuthNeeded = await api.isAuthenticationNeeded();
if(isAuthNeeded && inviteToken) {
setEntPage(Page.Setup)
}
else if(isAuthNeeded)
setEntPage(Page.Login);
}
} catch (e) {
toast.error("Error occured while checking Mizu API status, see console for mode details");
console.error(e);
} finally {
setIsLoading(false);
}
},[setEntPage]);
useEffect(() => {
determinePage();
}, [determinePage]);
useEffect(() => {
if(!location.pathname || location.pathname !== '/') {
return;
}
switch (entPage) {
case Page.Traffic:
navigate("/");
break;
case Page.Setup:
navigate(RouterRoutes.SETUP);
break;
case Page.Login:
navigate(RouterRoutes.LOGIN);
break;
default:
navigate(RouterRoutes.LOGIN);
}
// eslint-disable-next-line
},[entPage, location])
if (isLoading) {
return <LoadingOverlay/>;
}
return <Routes> return <Routes>
<Route path={"/"} element={<SystemViewer isFirstLogin={isFirstLogin} setIsFirstLogin={setIsFirstLogin}/>}> <Route path={"/"} element={<SystemViewer isFirstLogin={isFirstLogin} setIsFirstLogin={setIsFirstLogin}/>}>
<Route path={RouterRoutes.SETTINGS} element={<SettingsPage/>} /> {/*todo: set settings component*/} <Route path={RouterRoutes.SETTINGS} element={<AuthenticatedRoute><SettingsPage/></AuthenticatedRoute>} /> {/*todo: set settings component*/}
<Route path={"/"} element={<TrafficPage/>}/> <Route path={"/"} element={<AuthenticatedRoute><TrafficPage/></AuthenticatedRoute>}/>
</Route> </Route>
<Route path={RouterRoutes.SETUP+ "/:inviteToken"} element={<AuthPageBase><InstallPage onFirstLogin={() => setIsFirstLogin(true)}/></AuthPageBase>}/> <Route path={RouterRoutes.SETUP+ "/:inviteToken"} element={<AuthPageBase><InstallPage onFirstLogin={() => setIsFirstLogin(true)}/></AuthPageBase>}/>

View File

@@ -54,12 +54,12 @@ export const EntHeader: React.FC<EntHeaderProps> = ({isFirstLogin, setIsFirstLog
const ProfileButton = () => { const ProfileButton = () => {
const setEntPage = useSetRecoilState(entPageAtom); const navigate = useNavigate();
const logout = async (popupState) => { const logout = async (popupState) => {
try { try {
await api.logout(); await api.logout();
setEntPage(Page.Login); navigate(RouterRoutes.LOGIN);
} catch (e) { } catch (e) {
toast.error("Something went wrong, please check the console"); toast.error("Something went wrong, please check the console");
console.error(e); console.error(e);

View File

@@ -57,7 +57,6 @@ export const InstallPage: React.FC<InstallPageProps> = ({onFirstLogin}) => {
setEntPage(Page.Traffic); setEntPage(Page.Traffic);
if (!await api.isAuthenticationNeeded()) { if (!await api.isAuthenticationNeeded()) {
navigate('/'); navigate('/');
setEntPage(Page.Traffic);
onFirstLogin(); onFirstLogin();
} }
} catch (e) { } catch (e) {

View File

@@ -8,6 +8,7 @@ import entPageAtom, {Page} from "../../../recoil/entPage";
import {useSetRecoilState} from "recoil"; import {useSetRecoilState} from "recoil";
import useKeyPress from "../../../hooks/useKeyPress" import useKeyPress from "../../../hooks/useKeyPress"
import shortcutsKeyboard from "../../../configs/shortcutsKeyboard" import shortcutsKeyboard from "../../../configs/shortcutsKeyboard"
import { useNavigate } from "react-router-dom";
const api = Api.getInstance(); const api = Api.getInstance();
@@ -19,6 +20,8 @@ const LoginPage: React.FC = () => {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const formRef = useRef(null); const formRef = useRef(null);
const navigate = useNavigate();
const setEntPage = useSetRecoilState(entPageAtom); const setEntPage = useSetRecoilState(entPageAtom);
@@ -29,6 +32,7 @@ const LoginPage: React.FC = () => {
await api.login(username, password); await api.login(username, password);
if (!await api.isAuthenticationNeeded()) { if (!await api.isAuthenticationNeeded()) {
setEntPage(Page.Traffic); setEntPage(Page.Traffic);
navigate("/");
} else { } else {
toast.error("Invalid credentials"); toast.error("Invalid credentials");
} }

View File

@@ -0,0 +1,40 @@
import React, { ReactNode, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import LoadingOverlay from "../components/LoadingOverlay";
import Api from "./api";
import { RouterRoutes } from "./routes";
const api = Api.getInstance();
export const AuthenticatedRoute: React.FC = ({children}) => {
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
(async () => {
setIsLoading(true);
try {
const isInstallNeeded = await api.isInstallNeeded();
if (isInstallNeeded) {
navigate(RouterRoutes.SETUP);
} else {
const isAuthNeeded = await api.isAuthenticationNeeded();
if(isAuthNeeded) {
navigate(RouterRoutes.LOGIN);
}
}
} catch (e) {
console.error(e);
} finally {
setIsLoading(false);
}
})();
}, [])
if (isLoading) {
return <LoadingOverlay/>;
}
return <>{children}</>;
}

View File

@@ -219,7 +219,7 @@ export default class Api {
isAuthenticationNeeded = async () => { isAuthenticationNeeded = async () => {
try { try {
await this.client.get("/status/tap"); await this.client.get("/user/whoAmI");
return false; return false;
} catch (e) { } catch (e) {
if (e.response.status === 401) { if (e.response.status === 401) {