From aefcf3520b6c917f3c4be83dc443e6630f962029 Mon Sep 17 00:00:00 2001 From: Tejash Vaishnav <79075233+tejashVaishnav@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:54:26 +0530 Subject: [PATCH] Update options.ts added callback for next-auth to get logged-in user ID in session. --- app/api/auth/[...nextauth]/options.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/api/auth/[...nextauth]/options.ts b/app/api/auth/[...nextauth]/options.ts index c34c965..85f51df 100644 --- a/app/api/auth/[...nextauth]/options.ts +++ b/app/api/auth/[...nextauth]/options.ts @@ -11,4 +11,22 @@ export const authOptions: NextAuthOptions = { clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }), ], + callbacks: { + session: async ({ session, token }) => { + if (session?.user) { + //@ts-expect-error + session.user.id = token.sub; + } + return session; + }, + jwt: async ({ user, token }) => { + if (user) { + token.uid = user.id; + } + return token; + }, + }, + session: { + strategy: 'jwt', + }, };