Performance concern on Nextjs 13 layout files

Hello, I would like to ask the following questions regarding this code
export default async function AdminPagesLayout({ children }: PropsWithChildren) {
if (await isAdministrator()) return <>{children}</>;
logger.error('Unauthorized access to admin pages');
return (
<div className="h-screen w-screen flex flex-col gap-y-5 justify-center items-center">
🔒&nbsp;&nbsp;
<b className="text-lg">Unauthorized access to admin pages</b>
<Link href="/">
<Button>Go home</Button>
</Link>
</div>
);
}
export default async function AdminPagesLayout({ children }: PropsWithChildren) {
if (await isAdministrator()) return <>{children}</>;
logger.error('Unauthorized access to admin pages');
return (
<div className="h-screen w-screen flex flex-col gap-y-5 justify-center items-center">
🔒&nbsp;&nbsp;
<b className="text-lg">Unauthorized access to admin pages</b>
<Link href="/">
<Button>Go home</Button>
</Link>
</div>
);
}
First question would be, would you consider this a good solution to check whether the user would be an administrator? I know middleware is supposed to be a better candidate for a solution but I find layouts to be much easier. Second question would be, given this file tree (attached screenshot) do you think there would be a potential issue whenever someone changes route? I think there should not be a problem due to caching but I am just looking for people with different opinions so I can improve my current arch. Thanks for reading! t3stackminty
No description
1 Reply
Dimi
DimiOP•15mo ago
Regarding the second question, I would like to know if the database would perform the same query again and again on every redirect that the admin does. Inside the isAdministrator function lies a prisma call something like this
export async function isAdministrator(): Promise<boolean> {
const session = await getServerSession(authOptions);
if (!session || !session.user?.email) return false;
const user = await prisma.user.findUnique({ where: { email: session.user.email } });
console.log(!!user && user.type === "admin")
return !!user && user.type === "admin";
}
export async function isAdministrator(): Promise<boolean> {
const session = await getServerSession(authOptions);
if (!session || !session.user?.email) return false;
const user = await prisma.user.findUnique({ where: { email: session.user.email } });
console.log(!!user && user.type === "admin")
return !!user && user.type === "admin";
}
Want results from more Discord servers?
Add your server