"use server";
import { redirect } from "next/navigation";
import { completeSignIn, endSession, safeNext, signIn } from "@/lib/auth";
import { SIGNUPS_ENABLED } from "@/lib/signups";
import { createUser, UserError } from "@/lib/users";
import type { FormState } from "./types";
export async function signupAction(_prev: FormState, formData: FormData): Promise<FormState> {
if (!SIGNUPS_ENABLED) return { error: "Sign-ups are closed." };
const password = String(formData.get("password") ?? "");
try {
const user = await createUser({
username: String(formData.get("username") ?? ""),
email: String(formData.get("email") ?? ""),
password,
name: String(formData.get("name") ?? ""),
});
await signIn(user.email, password);
} catch (e) {
if (e instanceof UserError) return { error: e.message };
throw e;
}
redirect(safeNext(formData.get("next")));
}
export async function loginAction(_prev: FormState, formData: FormData): Promise<FormState> {
const result = await signIn(String(formData.get("login") ?? ""), String(formData.get("password") ?? ""));
if (!result) return { error: "Incorrect username or password." };
const next = safeNext(formData.get("next"));
if (result.needsCode) redirect(next === "/" ? "/login/two-factor" : `/login/two-factor?next=${encodeURIComponent(next)}`);
redirect(next);
}
export async function twoFactorLoginAction(_prev: FormState, formData: FormData): Promise<FormState> {
const next = safeNext(formData.get("next"));
const result = await completeSignIn(String(formData.get("code") ?? ""));
if (result === "invalid") return { error: "That code didn't match. Try the newest code from your app." };
if (result === "locked") return { error: "Too many wrong codes. Wait 15 minutes, then try again." };
if (result === "expired") redirect(`/login?expired=1${next !== "/" ? `&next=${encodeURIComponent(next)}` : ""}`);
redirect(next);
}
export async function logoutAction() {
await endSession();
redirect("/");
}