1
0
Fork 0

redirect if in weird states

This commit is contained in:
Julian 2023-04-26 14:19:58 -04:00
parent d0310ea0eb
commit dfaab3a290
Signed by untrusted user: NotNite
GPG Key ID: BD91A5402CCEB08A
4 changed files with 75 additions and 46 deletions

View File

@ -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<HTMLInputElement>(null);
const passwordRef = React.useRef<HTMLInputElement>(null);
return (
<form
style={{ display: "flex", flexDirection: "column" }}
onSubmit={async (e) => {
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
}
}}
>
<input type="text" placeholder="Username" ref={usernameRef} required />
<input
type="password"
placeholder="Password"
ref={passwordRef}
required
/>
<input type="submit" value="Login" />
</form>
);
}

View File

@ -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<HTMLInputElement>(null);
const passwordRef = React.useRef<HTMLInputElement>(null);
export default async function Page() {
const state = await getAuthState();
if (state === AuthState.Registering) redirect("/register");
if (state === AuthState.LoggedIn) redirect("/me");
return (
<main className={styles.main}>
<form
style={{ display: "flex", flexDirection: "column" }}
onSubmit={async (e) => {
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
}
}}
>
<input type="text" placeholder="Username" ref={usernameRef} required />
<input
type="password"
placeholder="Password"
ref={passwordRef}
required
/>
<input type="submit" value="Login" />
</form>
<LoginForm />
</main>
);
}

View File

@ -13,9 +13,7 @@ export default function Home() {
}}
>
<a href="/login">login</a>
<a href="/me">me</a>
<a href="/oauth/discord/login">discord</a>
<a href="/register">register</a>
<a href="/oauth/discord/login">register (discord)</a>
</p>
</main>
);

View File

@ -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;
}