What's the best way to get the userId in prisma/AuthJs?

Is there a better way to do this? Because I should make a request everytime just to get the userId. How do you do it? I'd create a state to store it when the user logs in and then use POST and body to then get it in the server. Is that the best way to do it?
export async function POST(request: Request) {
const session = await getServerSession(authOptions);

const getUserId = await prisma.user.findUnique({
where: { email: session!.user!.email! },
});

const newCategory = await prisma.todo.create({
data: {
pinned: false,
text: "Hi",
userId: getUserId!.id!,
},
});
return NextResponse.json({ getUserId, newCategory });
}
export async function POST(request: Request) {
const session = await getServerSession(authOptions);

const getUserId = await prisma.user.findUnique({
where: { email: session!.user!.email! },
});

const newCategory = await prisma.todo.create({
data: {
pinned: false,
text: "Hi",
userId: getUserId!.id!,
},
});
return NextResponse.json({ getUserId, newCategory });
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server