next-auth: Augmenting session type doesn't seem to work on Auth.js anymore

Question 💬

I want to pass the user ID to the session.user object. I can do this by using the session callback in the auth config:

callbacks: {
	async session({ session, user }) {
		if (session.user) {
			session.user.id = user.id;
		}
		return Promise.resolve(session);
	}
}

However, this throws a TypeScript error as session.user doesn’t have an ID field. To fix this, I followed the instructions here and created the following auth.d.ts file:

import type { DefaultSession } from '@auth/core/types';

declare module '@auth/core/types' {
	interface Session {
		user: {
			id: string;
		} & DefaultSession['user'];
	}
}

This worked just fine until I updated @auth/core to 0.4.0 and @auth/sveltekit to 0.2.1. Now no matter what I try (such as augmenting the CallbackOptions interface amongst other things), I cannot get TypeScript to know about the id field in session.user.

While normally I’d just @ts-expect-error and go about my day, this issue is quite major for me as I am accessing session.user.id in most of my backend routes to make appropriate DB calls and the like.

How to reproduce ☕️

I’m reusing the minimal reproduction repo I posted in my previous issue on edge functions and SvelteKit. I added a new commit to it that reproduces the issues I just talked about. Simply cloning, npm installing and opening in vscode reproduces the typescript errors for me in hooks.server.ts.

Contributing 🙌🏽

No, I am afraid I cannot help regarding this

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 22 (4 by maintainers)

Most upvoted comments

Thanks for taking a look. Can confirm this workaround does work (all that’s needed is changing @auth/core/types to @auth/sveltekit/node_modules/@auth/core/types actually, the second interface augmentation doesn’t seem to be necessary since I’m not adding custom fields, just including the id in the session).

It seems that since 0.4.0, there is a nested dependency in node_modules: Screen Shot 2023-02-11 at 22 20 29 While on 0.3.0 it doesn’t have this issue

Here is a possible workaround for you while waiting for us to patch this:

declare module '@auth/sveltekit/node_modules/@auth/core/types' {
	interface Session {
		user: {
			id: string;
		} & DefaultSession['user'];
	}
	interface User {
		foo: 'bar'
	}
}

Version 0.5.1 fixes this. You should now be able to override the module directly:

declare module '@auth/core/types' {
    // your codes here
}

Yup works fine for me after updating to @auth/core 0.5.1 and @auth/sveltekit 0.3.0. Thanks!

Seems to work ok without the override now using:

{
  "dependencies": {
    "@auth/core": "^0.10.0",
    "@auth/sveltekit": "^0.3.6"
  },
}

Although I had to remove @auth/core then re-add it to get it to update with pnpm.

Just in case anyone had the same issue as me in the future: I also had to update my src/lib/types/auth.d.ts to match yours from:

export declare module "@auth/core/types" {
	interface Session {
		user: {
			nickname?: string;
			projects?: string[];
			permissions?: string[];
		} & DefaultSession["user"];
	}
}

to:

import type { DefaultSession } from "@auth/core/types";

declare module "@auth/core/types" {
	interface Session {
		user?: {
			nickname?: string;
			projects?: string[];
			permissions?: string[];
		} & DefaultSession["user"];
	}
}

As @ArcadeArchie said, it seems to be a version mismatch with pnpm. This fixed it for me as well.

In package.json

{
	"dependencies": {
		"@auth/core": "^0.9.0",
		"@auth/sveltekit": "^0.3.5"
	},
	"pnpm": {
		"overrides": {
			"@auth/core": "^0.9.0"
		}
	}
}

In src/lib/types/auth.d.ts

import type { DefaultSession } from "@auth/core/types";

declare module "@auth/core/types" {
	interface Session {
		user?: {
			id: string;
		} & DefaultSession["user"];
	}
}

seemed to be an issue caused by a version mismatch between the @auth/core of @auth/sveltekit and @auth/core of my project, I’ve fixed by using pnpm’s override feature to force a single version of @auth/core

Not sure if this is a regression, or if I’m missing something - but I’m having this issue again. Can anyone else confirm?

{
	"@auth/core": "^0.8.1",
	"@auth/sveltekit": "^0.3.1",
	"@next-auth/prisma-adapter": "^1.0.7"
}

For types to augment, I’m having to revert back to duplicating my types.

declare module '@auth/core/types' {
	interface User {
		id: string;
		name: string | null;
		avatar: string | null;
	}
	interface Session {
		user: {
			foo: 'bar';
		};
	}
}


declare module '@auth/sveltekit/node_modules/@auth/core/types' {
	interface User {
		id: string;
		name: string | null;
		avatar: string | null;
	}
	interface Session {
		user: {
			foo: 'bar';
		};
	}
}

export {};

Also for pnpm note package name could be different, in my case I have latest nodemailer and so my package instead of @auth+core@0.3.0 is @auth+core@0.3.0_nodemailer@6.9.1 best way to figure out package is looking in lock file for the package related to @auth/core as a dependency of @auth/sveltekit.

Also a shortcut can be made in tsconfig of this path. I use the full path for type def but to reuse types in the code base I import from tsconfig alias path for me - paths : { @dependencies/types: [path] } works for me. Don’t forget to leave/ included your own or sveltekit alias paths

It seems that since 0.4.0, there is a nested dependency in node_modules: Screen Shot 2023-02-11 at 22 20 29 While on 0.3.0 it doesn’t have this issue Here is a possible workaround for you while waiting for us to patch this:

declare module '@auth/sveltekit/node_modules/@auth/core/types' {
	interface Session {
		user: {
			id: string;
		} & DefaultSession['user'];
	}
	interface User {
		foo: 'bar'
	}
}

does this work with pnpm? I don’t see a @auth/sveltekit/node_modules/@auth mine only has @auth/sveltekit/node_modules/.bin and I am not sure what to do with that.

For PNPM users with @authjs/sveltekit: 0.21 add this to your declaration .d.ts file and replace “<relative-path-to-node_modules>” with the appropriate value

import { DefaultSession } from "@auth/core";

declare module "<relative-path-to-node_modules>/.pnpm/@auth+core@0.3.0/node_modules/@auth/core/types" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session {
    // All of the Pages have SSR auth, so we can assume that the user is logged in (no need for optional type)
    user: {
      id: string;
    } & DefaultSession["user"];
  }
}

for my case, my folder structure was like this:

|
| node_modules
| src
| ── src/types
| ──── src/types/auth.d.ts

so I put this in auth.d.ts

import { DefaultSession } from "@auth/core";

declare module "../../node_modules/.pnpm/@auth+core@0.3.0/node_modules/@auth/core/types" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session {
    // All of the Pages have SSR auth, so we can assume that the user is logged in (no need for optional type)
    user: {
      id: string;
    } & DefaultSession["user"];
  }
}

also seems like sveltekit has a dependency on 0.3.0 instead of .0.4.0 which could be source of problem

That is why I put “Auth.js” in the title as I suspect this might not be a “NextAuth.js” issue. Also the augment worked just fine on @auth/core 0.3.0 and @auth/sveltekit 0.2.0, so it must’ve been a recent change that broke it.