Skip to content

Next Auth V5 Augment Typescript User Interface doesnt work #9253

@cexra

Description

@cexra

What is the improvement or update you wish to see?

next auth js provides documentation that does not work to augment the User interface in the @auth/core module.

Is there any context that might help us understand?

from the existing documentation we can augment the User interface like the method below

next-auth.d.ts

import { JWT } from '@auth/core/jwt'
import { type DefaultSession } from 'next-auth'

declare module '@auth/core' {
  interface Session {
    user: {
      id: string
      identifier: string
      role: 'USER' | 'ADMIN'
      name: string
    } & DefaultSession['user']
  }

  interface User {
    id: string
    identifier: string
    name: string
    role: 'USER' | 'ADMIN'
  }
}

declare module '@auth/core/jwt' {
  interface JWT {
    identifier: string
    name: string
    role: 'USER' | 'ADMIN'
  }
}

augment Session interface and JWT interface it works. but augment User interface does not work. my purpose of augmenting the User interface is to persist the role attribute on the User object, so that I can store it on the Session object.

auth.config.ts

import type { NextAuthConfig } from 'next-auth'

export const authConfig = {
  callbacks: {
    jwt({ token, user }) {
      if (user) token.role = user.role // Property 'role' does not exist on type 'User | AdapterUser'. Property 'role' does not exist on type 'User'.ts(2339)
      return token
    },

    session({ session, token }) {
      session.user.role = token.role
      return session
    },
  },
} satisfies NextAuthConfig

Does the docs page already exist? Please link to it.

https://authjs.dev/getting-started/typescript?frameworks=next

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsRelates to documentationtriageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions