diff --git a/src/app/login/LoginForm.tsx b/src/app/login/LoginForm.tsx new file mode 100644 index 0000000..6fa3ee6 --- /dev/null +++ b/src/app/login/LoginForm.tsx @@ -0,0 +1,49 @@ +"use client"; + +import React from "react"; + +// TODO: use input from register & un programmer art this +export default function LoginForm() { + const usernameRef = React.useRef(null); + const passwordRef = React.useRef(null); + + return ( +
{ + e.preventDefault(); + + const username = usernameRef.current?.value ?? ""; + const password = passwordRef.current?.value ?? ""; + + const req = await fetch("/api/login", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + username, + password + }) + }); + + if (req.status === 200) { + const res: { ticket: string } = await req.json(); + document.cookie = `ticket=${res.ticket}; path=/;`; + window.location.href = "/me"; + } else { + // todo error handling lol + } + }} + > + + + +
+ ); +} diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 538bd1c..9c7605c 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,52 +1,18 @@ -"use client"; - import styles from "@/app/page.module.css"; import React from "react"; +import LoginForm from "./LoginForm"; +import { AuthState, getAuthState } from "@/auth"; +import { redirect } from "next/navigation"; -// TODO: use input from register & un programmer art this -export default function Page() { - const usernameRef = React.useRef(null); - const passwordRef = React.useRef(null); +export default async function Page() { + const state = await getAuthState(); + + if (state === AuthState.Registering) redirect("/register"); + if (state === AuthState.LoggedIn) redirect("/me"); return (
-
{ - e.preventDefault(); - - const username = usernameRef.current?.value ?? ""; - const password = passwordRef.current?.value ?? ""; - - const req = await fetch("/api/login", { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - username, - password - }) - }); - - if (req.status === 200) { - const res: { ticket: string } = await req.json(); - document.cookie = `ticket=${res.ticket}; path=/;`; - window.location.href = "/me"; - } else { - // todo error handling lol - } - }} - > - - - -
+
); } diff --git a/src/app/page.tsx b/src/app/page.tsx index cb019ad..8e00a1e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -13,9 +13,7 @@ export default function Home() { }} > login - me - discord - register + register (discord)

); diff --git a/src/auth.ts b/src/auth.ts index 5941098..acaefd7 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -94,3 +94,19 @@ export async function createAuthTicket(username: string) { return authTicket.ticket; } + +export enum AuthState { + LoggedOut, + Registering, + LoggedIn +} + +export async function getAuthState() { + const user = await getUser(); + if (user === null) return AuthState.LoggedOut; + + const info = ldap.getUserInfo(user); + if (info === null) return AuthState.Registering; + + return AuthState.LoggedIn; +}