require auth
This commit is contained in:
26
src/RequireAuth.tsx
Normal file
26
src/RequireAuth.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import * as React from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useAuth, AuthPage } from "../react-auth";
|
||||
|
||||
export function RequireAuth({ children }: { children: React.ReactNode }) {
|
||||
const { currentUser, loading, error, login, register } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [mode, setMode] = React.useState<"login" | "register">("login");
|
||||
|
||||
if (currentUser) {
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthPage
|
||||
mode={mode}
|
||||
onBack={() => navigate("/")}
|
||||
onSwitchMode={() => setMode(mode === "login" ? "register" : "login")}
|
||||
login={login}
|
||||
register={register}
|
||||
loading={loading}
|
||||
error={error}
|
||||
currentUser={currentUser}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import Dashboard from './Dashboard';
|
||||
import FetchRequests from './FetchRequests';
|
||||
import FetchRequestDetail from './FetchRequestDetail';
|
||||
import ReportSnapshots from './ReportSnapshots';
|
||||
import { RequireAuth } from './RequireAuth';
|
||||
import { AppProvider, Admin } from '../react-openapi';
|
||||
import { Buffer } from 'buffer';
|
||||
import process from 'process';
|
||||
@@ -64,7 +65,7 @@ root.render(
|
||||
path={path}
|
||||
element={
|
||||
path.startsWith("/admin") ? (
|
||||
<Component basePath="/admin" />
|
||||
<RequireAuth><Component basePath="/admin" /></RequireAuth>
|
||||
) : (
|
||||
<Component />
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user