Next.js Discord

Discord Forum

Next Auth - enrich the JWT token with custom data

Answered
Oriental posted this in #help-forum
Open in Discord
Avatar
OrientalOP
Hey all!
I am using next-auth with Google OAUTH and using the JWT strategy.
Really struggling with adding extra information to the session.user object...
Any guidance please?
Answered by tafutada777
you can tailor it in options.ts as follows.

        callbacks: {
            jwt: async ({token, user, account, profile, isNewUser}) => {
                // Add role to the user info in the token right after sign in
                console.log('in jwt', {user, token, account, profile})

                if (user) {
                    token.user = user;
                    const u = user as any
                    token.role = u.role;
                }
                if (account) {
                    token.accessToken = account.access_token
                    token.refreshToken = account.refresh_token
                }
                return token;
            },
            session: ({session, token}) => {
                // console.log("in session", {session, token});
                token.accessToken
                return {
                    ...session,
                    user: {
                        ...session.user,
                        role: token.role,
                        accessToken: token.accessToken,
                        refreshToken: token.refreshToken,
                    },

                };
            },
        }


on top of that, you can retrieve the added properties like this:

export default async function Page() {
    const session = await getServerSession(options) // behind the scene, session cookie is accessed by Auth.js
    const user = session?.user
    user?.accessToken


for typescript, next-atuh.d.ts

import NextAuth, { DefaultSession } from "next-auth"

declare module "next-auth" {
    /**
     * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
     */
    interface Session {
        user: {
            /** The user's postal address. */
            accessToken: string,
            refreshToken: string,
        } & DefaultSession["user"]
    }
}
View full answer

2 Replies

Avatar
you can tailor it in options.ts as follows.

        callbacks: {
            jwt: async ({token, user, account, profile, isNewUser}) => {
                // Add role to the user info in the token right after sign in
                console.log('in jwt', {user, token, account, profile})

                if (user) {
                    token.user = user;
                    const u = user as any
                    token.role = u.role;
                }
                if (account) {
                    token.accessToken = account.access_token
                    token.refreshToken = account.refresh_token
                }
                return token;
            },
            session: ({session, token}) => {
                // console.log("in session", {session, token});
                token.accessToken
                return {
                    ...session,
                    user: {
                        ...session.user,
                        role: token.role,
                        accessToken: token.accessToken,
                        refreshToken: token.refreshToken,
                    },

                };
            },
        }


on top of that, you can retrieve the added properties like this:

export default async function Page() {
    const session = await getServerSession(options) // behind the scene, session cookie is accessed by Auth.js
    const user = session?.user
    user?.accessToken


for typescript, next-atuh.d.ts

import NextAuth, { DefaultSession } from "next-auth"

declare module "next-auth" {
    /**
     * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
     */
    interface Session {
        user: {
            /** The user's postal address. */
            accessToken: string,
            refreshToken: string,
        } & DefaultSession["user"]
    }
}
Answer
Avatar
OrientalOP
Thanks for the help!