first commit
This commit is contained in:
commit
b7b0f253ce
85 files changed
+7560
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { auth } from "@/auth";
|
||||
import { prisma } from "./db";
|
||||
|
||||
export async function requireUser() {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) redirect("/login");
|
||||
return session.user;
|
||||
}
|
||||
|
||||
/** Hämtar boken om den tillhör inloggad användare, annars 404. */
|
||||
export async function requireBook(bookId: string) {
|
||||
const user = await requireUser();
|
||||
const book = await prisma.book.findFirst({ where: { id: bookId, ownerId: user.id } });
|
||||
if (!book) notFound();
|
||||
return book;
|
||||
}
|
||||
|
||||
/** Hämtar en post i boken, annars 404. */
|
||||
export async function requireEntity(bookId: string, entityId: string) {
|
||||
await requireBook(bookId);
|
||||
const entity = await prisma.entity.findFirst({ where: { id: entityId, bookId } });
|
||||
if (!entity) notFound();
|
||||
return entity;
|
||||
}
|
||||
Reference in new issue
Block a user