Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending the Session in next-auth@^5.0.0-beta.25 is not working. #12714

Open
xgbnl opened this issue Feb 27, 2025 · 2 comments
Open

Extending the Session in next-auth@^5.0.0-beta.25 is not working. #12714

xgbnl opened this issue Feb 27, 2025 · 2 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@xgbnl
Copy link

xgbnl commented Feb 27, 2025

Environment

 System:
    OS: Linux 5.15 Ubuntu 24.04.2 LTS 24.04.2 LTS (Noble Numbat)
    CPU: (16) x64 13th Gen Intel(R) Core(TM) i5-13500H
    Memory: 6.25 GB / 7.58 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 22.12.0 - /usr/local/lib/nodejs/node-v22.12.0-linux-x64/bin/node
    npm: 11.1.0 - /usr/local/lib/nodejs/node-v22.12.0-linux-x64/bin/npm
  npmPackages:
    @auth/prisma-adapter: ^2.7.4 => 2.7.4
    next: ^15.2.0 => 15.2.0
    next-auth: ^5.0.0-beta.24 => 5.0.0-beta.25
    react: 18.3.1 => 18.3.1

Reproduction URL

https://github.com/xgbnl/vuexy-kit-template

Describe the issue

I’m using next-auth@^5.0.0-beta.25, and I’m trying to extend the Session object according to the official documentation, but it doesn’t seem to be working as expected.

How to reproduce

  1. I followed the next-auth v5 documentation (https://authjs.dev/getting-started/typescript?framework=next-js) to create a next-auth.d.ts file

next-auth.d.ts

import NextAuth, { type DefaultSession } from 'next-auth'

declare module 'next-auth' {
  /**
   * Returned by `auth`, `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session {
    user: {
      username: string
      avatar: string
    } & DefaultSession['user']
  }
}
  1. I wrote the necessary code inside it and added the file to the include array in my tsconfig.json
  "include": [
    "next.config.ts",
    "tailwind.config.ts",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts",
    "next-auth.d.ts"
  ],
  1. However, the session object within the callback function is not returning the custom fields I defined as expected
  callbacks: {
    /*
     * While using `jwt` as a strategy, `jwt()` callback will be called before
     * the `session()` callback. So we have to add custom parameters in `token`
     * via `jwt()` callback to make them accessible in the `session()` callback
     */
    async jwt({ token, user }) {
      if (user) {
        /*
         * For adding custom parameters to user in session, we first need to add those parameters
         * in token which then will be available in the `session()` callback
         */
        token.name = user.name
      }

      return token
    },
    async session({ session, token, user }) {
      console.log('Custom Session',session)
      if (session.user) {
        // ** Add custom params to user in session which are added in `jwt()` callback via `token` parameter
        session.user.name = token.name
      }

      return session
    }
  }
  1. Print Result
Custom Session {
  user: { name: undefined, email: undefined, image: undefined },
  expires: '2025-03-29T08:14:48.518Z'
}

Expected behavior

I’ve been working on this for a whole day without any success. I need your help.

@xgbnl xgbnl added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Feb 27, 2025
@r-bromfield
Copy link

@xgbnl you need to update the jwt submodule as well

see example below

// The `JWT` interface can be found in the `next-auth/jwt` submodule
import { JWT } from "next-auth/jwt"
 
declare module "next-auth/jwt" {
  /** Returned by the `jwt` callback and `auth`, when using JWT sessions */
  interface JWT {
    /** OpenID ID Token */
    idToken?: string
   
  }
}

@xgbnl
Copy link
Author

xgbnl commented Mar 4, 2025

@xgbnl you need to update the jwt submodule as well

see example below

// The `JWT` interface can be found in the `next-auth/jwt` submodule
import { JWT } from "next-auth/jwt"
 
declare module "next-auth/jwt" {
  /** Returned by the `jwt` callback and `auth`, when using JWT sessions */
  interface JWT {
    /** OpenID ID Token */
    idToken?: string
   
  }
}

Thank you, I have resolved this issue, and it helps me get the IDE to suggest the object’s properties. I encountered a new problem where using throw new Error('error content') in the authorize() method of version beta25 seems to not be caught when using the signIn() method in the Login.tsx component. Do you have any good solutions for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants