Create a context to store authentication
This commit is contained in:
@ -1,45 +1,61 @@
|
||||
import React from "react";
|
||||
import { Route, Routes } from "react-router-dom";
|
||||
import "./App.css";
|
||||
import { AuthApi } from "./api/AuthApi";
|
||||
import { NotFoundRoute } from "./routes/NotFound";
|
||||
import { BaseLoginPage } from "./widgets/BaseLoginpage";
|
||||
import { ProfileRoute } from "./routes/ProfileRoute";
|
||||
import { LoginRoute } from "./routes/auth/LoginRoute";
|
||||
import { NewAccountRoute } from "./routes/auth/NewAccountRoute";
|
||||
import { OIDCCbRoute } from "./routes/auth/OIDCCbRoute";
|
||||
import { useAtom } from "jotai";
|
||||
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
|
||||
import { PasswordForgottenRoute } from "./routes/auth/PasswordForgottenRoute";
|
||||
import { ResetPasswordRoute } from "./routes/auth/ResetPasswordRoute";
|
||||
import { NewAccountRoute } from "./routes/auth/NewAccountRoute";
|
||||
import { ProfileRoute } from "./routes/ProfileRoute";
|
||||
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);
|
||||
|
||||
/**
|
||||
* Core app
|
||||
*/
|
||||
function App() {
|
||||
const [signedIn] = useAtom(AuthApi.authStatus);
|
||||
export function App(): React.ReactElement {
|
||||
const [signedIn, setSignedIn] = React.useState(AuthApi.SignedIn);
|
||||
|
||||
const context: AuthContext = {
|
||||
signedIn: signedIn,
|
||||
setSignedIn: (s) => setSignedIn(s),
|
||||
};
|
||||
|
||||
return (
|
||||
<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 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>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
export function useAuth(): AuthContext {
|
||||
return React.useContext(AuthContextK)!;
|
||||
}
|
||||
|
Reference in New Issue
Block a user