next auth: USER ID
Answered
Blanc de Hotot posted this in #help-forum
Blanc de HototOP
I'm working on an API endpoint using Next.js and Prisma to create job entries in a database. The API endpoint is functioning correctly, but I'm encountering an issue where I need to retrieve the user ID from the session. This user ID is crucial for associating the job entry with the correct owner.
I have a session management system in place, but I'm having trouble accessing the user ID from the session within my POST handler. Without this information, I'm unable to establish the relationship between the job and the owner.
Could you please provide guidance or assistance on how to retrieve the user ID from the session data? I would greatly appreciate any help or code examples that could help me resolve this issue and successfully connect the job entry with the appropriate owner.
I have a session management system in place, but I'm having trouble accessing the user ID from the session within my POST handler. Without this information, I'm unable to establish the relationship between the job and the owner.
Could you please provide guidance or assistance on how to retrieve the user ID from the session data? I would greatly appreciate any help or code examples that could help me resolve this issue and successfully connect the job entry with the appropriate owner.
Answered by tafutada777
@Blanc de Hotot you did not mention if you use next-auth(Auth.js), so assuming that you use next-auth with PrismaAdapter, you can explicitly copy id from User table in options.ts > callbacks > session like this:
3 Replies
@Blanc de Hotot you did not mention if you use next-auth(Auth.js), so assuming that you use next-auth with PrismaAdapter, you can explicitly copy id from User table in options.ts > callbacks > session like this:
Answer
user(AdapterUser) has id property, so you can copy it to session, consequently you can grab the id from getServerSession(options)
export async function GET() {
const session = await getServerSession(options)
console.log('API session>', session)Blanc de HototOP
ty very much