Updated login page, added ProtectedRoute object for wrapping in layouts, added ProtectedRoute authorization verification to Admin route root
This commit is contained in:
parent
f055e77eea
commit
efa42a53b2
5 changed files with 65 additions and 17 deletions
|
|
@ -1,11 +1,13 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
export default function AdminLayout({children}: {children: React.ReactNode}) {
|
import ProtectedRoute from "@/components/wrappers/ProtectedRoute";
|
||||||
|
|
||||||
|
export default async function AdminLayout({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
// TODO: Put user state management here
|
// TODO: Put user state management here
|
||||||
//redirect("wherever state says") <= stateRouteManager()
|
//redirect("wherever state says") <= stateRouteManager()
|
||||||
return (
|
return <ProtectedRoute>{children}</ProtectedRoute>;
|
||||||
<div>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import { redirect } from "next/navigation";
|
import UnderConstruction from "@/components/static_pages/UnderConstruction";
|
||||||
|
|
||||||
export default function Admin() {
|
export default function Admin() {
|
||||||
// TODO: Replace with Layout State Manager
|
return <UnderConstruction />;
|
||||||
redirect("/Admin/Login");
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,16 @@
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
import { login, signup } from "@/lib/server_functions/userAuth";
|
import { login, signup } from "@/lib/server_functions/userAuth";
|
||||||
|
import Form from "@/components/ui/Form";
|
||||||
export default function Login() {
|
export default function Login() {
|
||||||
const [createAccount, setCreateAccount] = useState(false);
|
const [createAccount, setCreateAccount] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="flex h-full w-full justify-center py-10">
|
||||||
<form action={createAccount ? login : signup}>
|
<Form
|
||||||
|
action={createAccount ? login : signup}
|
||||||
|
submitText={createAccount ? "Login" : "Signup"}
|
||||||
|
>
|
||||||
<input type="text" name="username" placeholder="Username" required />
|
<input type="text" name="username" placeholder="Username" required />
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
|
|
@ -15,11 +19,12 @@ export default function Login() {
|
||||||
placeholder="Password"
|
placeholder="Password"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<button type="submit">{createAccount ? "Login" : "Signup"}</button>
|
|
||||||
</form>
|
|
||||||
<button onClick={() => setCreateAccount(!createAccount)}>
|
<button onClick={() => setCreateAccount(!createAccount)}>
|
||||||
{createAccount ? "Don't have an account?" : "Already have an account?"}
|
{createAccount
|
||||||
|
? "Don't have an account?"
|
||||||
|
: "Already have an account?"}
|
||||||
</button>
|
</button>
|
||||||
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
25
components/ui/Form.tsx
Normal file
25
components/ui/Form.tsx
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
|
import RoundedContainer from "./RoundedContainer";
|
||||||
|
|
||||||
|
export default function Form({
|
||||||
|
action,
|
||||||
|
children,
|
||||||
|
submitText,
|
||||||
|
}: {
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
action?: string | ((formData: FormData) => void | Promise<void>);
|
||||||
|
children: React.ReactNode;
|
||||||
|
submitText: string;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<form action={action}>
|
||||||
|
<RoundedContainer slim={false}>
|
||||||
|
{children}
|
||||||
|
<button type="submit">{submitText}</button>
|
||||||
|
</RoundedContainer>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
17
components/wrappers/ProtectedRoute.tsx
Normal file
17
components/wrappers/ProtectedRoute.tsx
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import React from "react";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
|
import { verifySession } from "@/lib/server_functions/dataAccessLayer";
|
||||||
|
|
||||||
|
export default async function ProtectedRoute({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
|
const session = await verifySession();
|
||||||
|
if (!session.isAuthed) {
|
||||||
|
redirect("/Authentication/Login");
|
||||||
|
}
|
||||||
|
|
||||||
|
return <div>{children}</div>;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue