35 lines
998 B
TypeScript
35 lines
998 B
TypeScript
"use server";
|
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { getAdminByUsername, getUserByUsername } from "@/lib/store";
|
|
import { verifyPassword } from "@/lib/password";
|
|
import { createAdminSession, createUserSession } from "@/lib/session";
|
|
|
|
export async function loginAction(formData: FormData) {
|
|
const username = String(formData.get("username") ?? "").trim();
|
|
const password = String(formData.get("password") ?? "");
|
|
|
|
const admin = await getAdminByUsername(username);
|
|
if (admin) {
|
|
const ok = await verifyPassword(password, admin.password_hash);
|
|
if (ok) {
|
|
await createAdminSession(admin.id);
|
|
redirect("/admin/dashboard");
|
|
}
|
|
}
|
|
|
|
const user = await getUserByUsername(username);
|
|
if (!user) {
|
|
redirect("/login?error=1");
|
|
}
|
|
|
|
const ok = await verifyPassword(password, user.password_hash);
|
|
if (!ok || !user.enabled) {
|
|
redirect("/login?error=1");
|
|
}
|
|
|
|
await createUserSession(user.id);
|
|
redirect(user.is_admin ? "/admin/dashboard" : "/me");
|
|
}
|