forked from NotNet/gluestick
redirect if in weird states
This commit is contained in:
parent
d0310ea0eb
commit
dfaab3a290
|
@ -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>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,52 +1,18 @@
|
||||||
"use client";
|
|
||||||
|
|
||||||
import styles from "@/app/page.module.css";
|
import styles from "@/app/page.module.css";
|
||||||
import React from "react";
|
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 async function Page() {
|
||||||
export default function Page() {
|
const state = await getAuthState();
|
||||||
const usernameRef = React.useRef<HTMLInputElement>(null);
|
|
||||||
const passwordRef = React.useRef<HTMLInputElement>(null);
|
if (state === AuthState.Registering) redirect("/register");
|
||||||
|
if (state === AuthState.LoggedIn) redirect("/me");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className={styles.main}>
|
<main className={styles.main}>
|
||||||
<form
|
<LoginForm />
|
||||||
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>
|
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,7 @@ export default function Home() {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<a href="/login">login</a>
|
<a href="/login">login</a>
|
||||||
<a href="/me">me</a>
|
<a href="/oauth/discord/login">register (discord)</a>
|
||||||
<a href="/oauth/discord/login">discord</a>
|
|
||||||
<a href="/register">register</a>
|
|
||||||
</p>
|
</p>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
|
|
16
src/auth.ts
16
src/auth.ts
|
@ -94,3 +94,19 @@ export async function createAuthTicket(username: string) {
|
||||||
|
|
||||||
return authTicket.ticket;
|
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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue