routing fix

This commit is contained in:
Leon
2022-02-02 14:25:39 +02:00
parent b9107a9fde
commit 2a11cb9dc5
2 changed files with 42 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
import React, {useCallback, useEffect, useState} from "react"; import React, {useCallback, useEffect, useState} from "react";
import {Route, Routes, useNavigate} from "react-router-dom"; import {Route, Routes, useLocation, useNavigate, useParams, useSearchParams} from "react-router-dom";
import {RouterRoutes} from "../helpers/routes"; import {RouterRoutes} from "../helpers/routes";
import {useRecoilState} from "recoil"; import {useRecoilState} from "recoil";
import entPageAtom, {Page} from "../recoil/entPage"; import entPageAtom, {Page} from "../recoil/entPage";
@@ -22,6 +22,10 @@ const AppSwitchRoutes = () => {
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [entPage, setEntPage] = useRecoilState(entPageAtom); 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 () => { const determinePage = useCallback(async () => {
try { try {
@@ -30,9 +34,11 @@ const AppSwitchRoutes = () => {
setEntPage(Page.Setup); setEntPage(Page.Setup);
} else { } else {
const isAuthNeeded = await api.isAuthenticationNeeded(); const isAuthNeeded = await api.isAuthenticationNeeded();
if(isAuthNeeded) { if(isAuthNeeded && inviteToken) {
setEntPage(Page.Login); setEntPage(Page.Setup)
} }
else if(isAuthNeeded)
setEntPage(Page.Login);
} }
} catch (e) { } catch (e) {
toast.error("Error occured while checking Mizu API status, see console for mode details"); toast.error("Error occured while checking Mizu API status, see console for mode details");
@@ -47,6 +53,10 @@ const AppSwitchRoutes = () => {
}, [determinePage]); }, [determinePage]);
useEffect(() => { useEffect(() => {
if(!location.pathname || location.pathname !== '/') {
return;
}
switch (entPage) { switch (entPage) {
case Page.Traffic: case Page.Traffic:
navigate("/"); navigate("/");
@@ -60,8 +70,8 @@ const AppSwitchRoutes = () => {
default: default:
navigate(RouterRoutes.LOGIN); navigate(RouterRoutes.LOGIN);
} }
// eslint-disable-next-line // eslint-disable-next-line
},[entPage]) },[entPage, location])
if (isLoading) { if (isLoading) {
@@ -71,11 +81,12 @@ const AppSwitchRoutes = () => {
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={<SettingsPage/>} /> {/*todo: set settings component*/}
<Route path={"/"} element={<TrafficPage/>} /> <Route path={"/"} element={<TrafficPage/>}/>
</Route> </Route>
<Route path={RouterRoutes.SETUP+ "/:inviteToken"} element={<AuthPageBase><InstallPage onFirstLogin={() => setIsFirstLogin(true)}/></AuthPageBase>}/>
<Route path={RouterRoutes.LOGIN} element={<AuthPageBase><LoginPage/></AuthPageBase>}/> <Route path={RouterRoutes.LOGIN} element={<AuthPageBase><LoginPage/></AuthPageBase>}/>
<Route path={RouterRoutes.SETUP} element={<AuthPageBase><InstallPage onFirstLogin={() => setIsFirstLogin(true)}/></AuthPageBase>}/>
</Routes> </Routes>
} }

View File

@@ -9,6 +9,8 @@ import {useSetRecoilState} from "recoil";
import entPageAtom, {Page} from "../../../recoil/entPage"; import entPageAtom, {Page} from "../../../recoil/entPage";
import useKeyPress from "../../../hooks/useKeyPress" import useKeyPress from "../../../hooks/useKeyPress"
import shortcutsKeyboard from "../../../configs/shortcutsKeyboard" import shortcutsKeyboard from "../../../configs/shortcutsKeyboard"
import { useNavigate, useParams } from "react-router-dom";
const api = Api.getInstance(); const api = Api.getInstance();
@@ -24,7 +26,10 @@ export const InstallPage: React.FC<InstallPageProps> = ({onFirstLogin}) => {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [passwordConfirm, setPasswordConfirm] = useState(""); const [passwordConfirm, setPasswordConfirm] = useState("");
const {inviteToken} = useParams()
const navigate = useNavigate();
console.log("inviteToken : ",inviteToken)
const setEntPage = useSetRecoilState(entPageAtom); const setEntPage = useSetRecoilState(entPageAtom);
const onFormSubmit = async () => { const onFormSubmit = async () => {
@@ -36,10 +41,22 @@ export const InstallPage: React.FC<InstallPageProps> = ({onFirstLogin}) => {
return; return;
} }
if(inviteToken){
registerUser(async ()=> await api.recoverUser({password: password, inviteToken: inviteToken}))
}
else{
registerUser(async () => await api.setupAdminUser(adminUsername, password))
}
}
const registerUser = async(registerFunc) => {
try { try {
setIsLoading(true);
await api.setupAdminUser(adminUsername, password); setIsLoading(true);
await registerFunc();
setEntPage(Page.Traffic);
if (!await api.isAuthenticationNeeded()) { if (!await api.isAuthenticationNeeded()) {
navigate('/');
setEntPage(Page.Traffic); setEntPage(Page.Traffic);
onFirstLogin(); onFirstLogin();
} }
@@ -55,7 +72,6 @@ export const InstallPage: React.FC<InstallPageProps> = ({onFirstLogin}) => {
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
} }
useKeyPress(shortcutsKeyboard.enter, onFormSubmit, formRef.current); useKeyPress(shortcutsKeyboard.enter, onFormSubmit, formRef.current);
@@ -63,11 +79,12 @@ export const InstallPage: React.FC<InstallPageProps> = ({onFirstLogin}) => {
return <div className="centeredForm" ref={formRef}> return <div className="centeredForm" ref={formRef}>
{isLoading && <LoadingOverlay/>} {isLoading && <LoadingOverlay/>}
<div className="form-title left-text">Setup</div> <div className="form-title left-text">Setup</div>
<span className="form-subtitle">Welcome to Mizu, please set up the admin user to continue</span> {inviteToken && <span className="form-subtitle">you have been invited to join Mizu, please set password in order to complete the registration process</span>}
<div className="form-input"> {!inviteToken && <span className="form-subtitle">Welcome to Mizu, please set up the admin user to continue</span>}
{!inviteToken && <div className="form-input">
<label htmlFor="inputUsername">Username</label> <label htmlFor="inputUsername">Username</label>
<input id="inputUsername" className={classes.textField} value={adminUsername} disabled={true} /> <input id="inputUsername" className={classes.textField} value={adminUsername} disabled={true} />
</div> </div>}
<div className="form-input"> <div className="form-input">
<label htmlFor="inputUsername">Password</label> <label htmlFor="inputUsername">Password</label>
<input id="inputUsername" className={classes.textField} value={password} type="password" onChange={(event) => setPassword(event.target.value)}/> <input id="inputUsername" className={classes.textField} value={password} type="password" onChange={(event) => setPassword(event.target.value)}/>