Files
WatchLink/src/lib/user-actions.ts

81 lines
2.0 KiB
TypeScript
Raw Normal View History

2026-05-15 03:11:41 +02:00
"use server";
import { compare, hash } from "bcryptjs";
import { redirect } from "next/navigation";
2026-05-15 20:13:29 +02:00
import { Prisma, type User } from "@prisma/client";
2026-05-15 03:11:41 +02:00
import { prisma } from "./prisma";
2026-05-15 20:13:29 +02:00
import { clearSession, setSession } from "./session";
import { getAppSettings } from "./settings";
2026-05-15 03:11:41 +02:00
function normalizeUsername(value: FormDataEntryValue | null) {
return String(value || "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9_-]/g, "");
}
export async function registerUser(formData: FormData) {
const settings = await getAppSettings();
if (settings.registrationMode !== "OPEN") {
redirect("/register?error=closed");
}
2026-05-15 03:11:41 +02:00
const username = normalizeUsername(formData.get("username"));
const password = String(formData.get("password") || "");
if (!username || password.length < 10) {
2026-05-15 20:13:29 +02:00
redirect("/register?error=invalid");
2026-05-15 03:11:41 +02:00
}
const passwordHash = await hash(password, 12);
2026-05-15 20:13:29 +02:00
let user: User;
try {
user = await prisma.user.create({
data: {
username,
displayName: username,
passwordHash,
ownedRooms: {
create: {
slug: `@${username}`,
name: `${username}'s room`,
visibility: "FRIENDS"
}
2026-05-15 03:11:41 +02:00
}
}
2026-05-15 20:13:29 +02:00
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
redirect("/register?error=username");
2026-05-15 03:11:41 +02:00
}
2026-05-15 20:13:29 +02:00
throw error;
}
2026-05-15 03:11:41 +02:00
await setSession(user.id);
redirect("/dashboard");
}
export async function loginUser(formData: FormData) {
const username = normalizeUsername(formData.get("username"));
const password = String(formData.get("password") || "");
const user = await prisma.user.findUnique({ where: { username } });
if (!user) {
2026-05-15 20:13:29 +02:00
redirect("/login?error=credentials");
2026-05-15 03:11:41 +02:00
}
const ok = await compare(password, user.passwordHash);
if (!ok) {
2026-05-15 20:13:29 +02:00
redirect("/login?error=credentials");
2026-05-15 03:11:41 +02:00
}
await setSession(user.id);
redirect("/dashboard");
}
2026-05-15 20:13:29 +02:00
export async function logoutUser() {
await clearSession();
redirect("/login");
}