I wanted to know if this method is optimal or not. Because I did not find any documentation about it.
example
a server action to get the current user information
actions/whoami.ts
"use server";
import { auth, prisma } from "@/lib/auth"; // next-auth config file
export default async function whoami(): Promis<User | null> {
const session = await auth();
if (!session?.user || !session.user.email) {
return null;
}
const user = await prisma.user.findUnique({
where: {
email: session.user.email,
},
});
return user;
}
use in client components:
export default function ClientPage() {
const { data: user } = useSWR("whoami", whoami);
console.log({ user }); // user is fully typed
return "...";
}
use in server components:
export default async function ServerPage() {
const user = await whoami();
console.log({ user }); // user is fully typed
return "...";
}