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)
Thanks for taking a look. Can confirm this workaround does work (all that’s needed is changing
@auth/core/typesto@auth/sveltekit/node_modules/@auth/core/typesactually, 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
While on
0.4.0, there is a nested dependency innode_modules:0.3.0it doesn’t have this issueHere is a possible workaround for you while waiting for us to patch this:
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:
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.tsto match yours from:to:
As @ArcadeArchie said, it seems to be a version mismatch with pnpm. This fixed it for me as well.
In
package.jsonIn
src/lib/types/auth.d.tsseemed 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?
For types to augment, I’m having to revert back to duplicating my types.
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 pathsFor PNPM users with
@authjs/sveltekit: 0.21add this to your declaration.d.tsfile and replace “<relative-path-to-node_modules>” with the appropriate valuefor my case, my folder structure was like this:
so I put this in auth.d.ts
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/core0.3.0 and@auth/sveltekit0.2.0, so it must’ve been a recent change that broke it.