GeneIT/geneit_app/src/App.tsx

62 lines
2.0 KiB
TypeScript
Raw Normal View History

import React from "react";
2023-06-06 14:39:47 +00:00
import { Route, Routes } from "react-router-dom";
import "./App.css";
import { AuthApi } from "./api/AuthApi";
import { NotFoundRoute } from "./routes/NotFound";
import { ProfileRoute } from "./routes/ProfileRoute";
2023-06-09 08:45:01 +00:00
import { LoginRoute } from "./routes/auth/LoginRoute";
import { NewAccountRoute } from "./routes/auth/NewAccountRoute";
2023-06-09 08:45:01 +00:00
import { OIDCCbRoute } from "./routes/auth/OIDCCbRoute";
2023-06-09 16:55:36 +00:00
import { PasswordForgottenRoute } from "./routes/auth/PasswordForgottenRoute";
2023-06-12 14:25:38 +00:00
import { ResetPasswordRoute } from "./routes/auth/ResetPasswordRoute";
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { BaseLoginPage } from "./widgets/BaseLoginpage";
interface AuthContext {
signedIn: boolean;
setSignedIn: (signedIn: boolean) => void;
}
const AuthContextK = React.createContext<AuthContext | null>(null);
2023-06-06 08:52:50 +00:00
2023-06-09 16:55:36 +00:00
/**
* Core app
*/
export function App(): React.ReactElement {
const [signedIn, setSignedIn] = React.useState(AuthApi.SignedIn);
const context: AuthContext = {
signedIn: signedIn,
setSignedIn: (s) => setSignedIn(s),
};
2023-06-09 08:45:01 +00:00
2023-06-06 08:52:50 +00:00
return (
<AuthContextK.Provider value={context}>
<Routes>
{signedIn ? (
<Route path="*" element={<BaseAuthenticatedPage />}>
<Route path="profile" element={<ProfileRoute />} />
<Route path="*" element={<NotFoundRoute />} />
</Route>
) : (
<Route path="*" element={<BaseLoginPage />}>
<Route path="" element={<LoginRoute />} />
<Route path="oidc_cb" element={<OIDCCbRoute />} />
<Route path="new-account" element={<NewAccountRoute />} />
<Route
path="password_forgotten"
element={<PasswordForgottenRoute />}
/>
<Route path="reset_password" element={<ResetPasswordRoute />} />
<Route path="*" element={<NotFoundRoute />} />
</Route>
)}
</Routes>
</AuthContextK.Provider>
2023-06-06 08:52:50 +00:00
);
}
export function useAuth(): AuthContext {
return React.useContext(AuthContextK)!;
}