At the moment my app look like this. i'm just wondering if there a better way?!
actions/getCurrentUser.ts
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import prisma from "@/app/libs/prismadb";
export async function getSession() return await getServerSession(authOptions);
export default async function getCurrentUser() {
const session = await getSession();
if (!session?.user?.email) return null;
const currentUser = await prisma.user.findUnique({
where: { email: session.user.email as string },
});
return { ...currentUser,
createdAt: currentUser.createdAt.toISOString(),
updatedAt: currentUser.updatedAt.toISOString(),
emailVerified: currentUser.emailVerified?.toISOString() || null,
};
/app/layout.tsx
export default async function RootLayout({ children }: {
children: React.ReactNode }) {
const currentUser = await getCurrentUser();
return <html lang="en">
<body className={font.className}>
<ClientOnly>
<Navbar currentUser={currentUser} />
</ClientOnly>
<div className="pb-20 pt-28">{children}</div>
</body>
</html>
}