Files
MIS-Contro-Tower/app/(app)/layout.tsx

32 lines
1.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { AppShell } from "@/components/layout/AppShell";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { prisma } from "@/lib/prisma";
const COOKIE_NAME = "mis_session";
export default async function AppLayout({ children }: { children: React.ReactNode }) {
const cookieJar = await cookies();
const sessionId = cookieJar.get(COOKIE_NAME)?.value;
const themeCookie = cookieJar.get("mis_theme")?.value;
const initialTheme = themeCookie === "light" ? "light" : "dark";
if (!sessionId) redirect("/login?next=/machines");
// validate session in DB (dont trust cookie existence)
const session = await prisma.session.findFirst({
where: {
id: sessionId,
revokedAt: null,
expiresAt: { gt: new Date() },
},
include: { user: true, org: true },
});
if (!session || !session.user?.isActive || !session.user?.emailVerifiedAt) {
redirect("/login?next=/machines");
}
return <AppShell initialTheme={initialTheme}>{children}</AppShell>;
}