Complete WatchLink V1 realtime features
This commit is contained in:
@@ -16,34 +16,64 @@ function normalizeUsername(value: FormDataEntryValue | null) {
|
||||
|
||||
export async function registerUser(formData: FormData) {
|
||||
const settings = await getAppSettings();
|
||||
if (settings.registrationMode !== "OPEN") {
|
||||
if (settings.registrationMode === "DISABLED") {
|
||||
redirect("/register?error=closed");
|
||||
}
|
||||
|
||||
const username = normalizeUsername(formData.get("username"));
|
||||
const password = String(formData.get("password") || "");
|
||||
const inviteCode = String(formData.get("inviteCode") || "").trim();
|
||||
|
||||
if (!username || password.length < 10) {
|
||||
redirect("/register?error=invalid");
|
||||
}
|
||||
|
||||
const invite =
|
||||
settings.registrationMode === "INVITE_ONLY"
|
||||
? await prisma.invite.findUnique({ where: { code: inviteCode } })
|
||||
: null;
|
||||
|
||||
if (
|
||||
settings.registrationMode === "INVITE_ONLY" &&
|
||||
(!invite || invite.status !== "ACTIVE" || (invite.expiresAt && invite.expiresAt.getTime() < Date.now()))
|
||||
) {
|
||||
redirect("/register?error=invite");
|
||||
}
|
||||
|
||||
const passwordHash = await hash(password, 12);
|
||||
let user: User;
|
||||
|
||||
try {
|
||||
user = await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
displayName: username,
|
||||
passwordHash,
|
||||
ownedRooms: {
|
||||
create: {
|
||||
slug: `@${username}`,
|
||||
name: `${username}'s room`,
|
||||
visibility: "FRIENDS"
|
||||
user = await prisma.$transaction(async (tx) => {
|
||||
const created = await tx.user.create({
|
||||
data: {
|
||||
username,
|
||||
displayName: username,
|
||||
passwordHash,
|
||||
ownedRooms: {
|
||||
create: {
|
||||
slug: `@${username}`,
|
||||
name: `${username}'s room`,
|
||||
isPersonal: true,
|
||||
visibility: "FRIENDS"
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (invite) {
|
||||
await tx.invite.update({
|
||||
where: { id: invite.id },
|
||||
data: { status: "USED", usedById: created.id, usedAt: new Date() }
|
||||
});
|
||||
if (invite.roomId) {
|
||||
await tx.roomMember.upsert({
|
||||
where: { roomId_userId: { roomId: invite.roomId, userId: created.id } },
|
||||
update: {},
|
||||
create: { roomId: invite.roomId, userId: created.id }
|
||||
});
|
||||
}
|
||||
}
|
||||
return created;
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
|
||||
@@ -64,6 +94,9 @@ export async function loginUser(formData: FormData) {
|
||||
if (!user) {
|
||||
redirect("/login?error=credentials");
|
||||
}
|
||||
if (user.disabledAt) {
|
||||
redirect("/login?error=disabled");
|
||||
}
|
||||
|
||||
const ok = await compare(password, user.passwordHash);
|
||||
if (!ok) {
|
||||
|
||||
Reference in New Issue
Block a user