1

Basic information: The app is using NextJS 13 with app router, Prisma for db querying, NextAuth for auth, Planetscale for db hosting.

I want to identify the user from the session object and then perform a query using Prisma in an api route. Of course, in most cases, I can just take the email from session.users which I get from const session = await getServerSession(authOptions); and query the prisma client to fetch the userId of the person with whom the email is associated.

I do also understand that session.users returns an object containing the email, profile picture and name. However, the email can be missing in case the user has decided to private their email on their respective oauth (for example, you can hide it on Github) which makes it so the session.users returns an object with no email and hence no way to identify the user (at least easily).

I do have a few solutions in mind which I will try soon but I'm not sure if they're efficient as they might require a few extra queries. Is there any short and fast way to identify users without their email?

CarboxyDev
  • 47
  • 1
  • 5

1 Answers1

0

You can extract user ID by customizing session callback.

Check the link below:

Get User ID from session in next-auth client

Ryan Ro
  • 304
  • 2
  • 11