Next Auth V5 Augment Typescript User Interface doesnt work
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
@cexra Try using @auth/core/types rather than @auth/core, that worked for me.
yes the solution works but only partially, the solution solves the problem on jwt and session callback. but why user.name is still a string | null | undefined when I think user.name should be a string only. can anyone help me how to augment this type
import "next-auth";
declare module "next-auth" {
interface User {
role: "SUPER" | "USER";
}
interface Session {
user?: User;
}
}
declare module "@auth/core/jwt" {
interface JWT {
role: "SUPER" | "USER";
}
}
This works for me.
I tried both ways, and i run into the same issue:
Property 'id' does not exist on type '{ name?: string | null | undefined; email?: string | null | undefined; image?: string | null | undefined; }'.
import NextAuth from "next-auth";
import Discord from "next-auth/providers/discord";
import type { NextAuthConfig } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db } from "@/app/db";
import { pgTable } from "./db/schema";
import { env } from "@/env.mjs";
enum UserRole {
Admin = "admin",
User = "user",
Free = "free",
}
declare module "@auth/core/types" {
interface Session {
user: {
id: string;
// ...other properties
role: UserRole;
} & DefaultSession["user"];
}
interface User {
// ...other properties
role: UserRole;
}
}
export const config = {
theme: {
logo: "https://next-auth.js.org/img/logo/logo-sm.png",
},
providers: [
Discord({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
adapter: DrizzleAdapter(db, pgTable),
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
role:session.user?.role
},
}),
},
} satisfies NextAuthConfig;
export const { handlers, auth } = NextAuth(config);
Have you tried to put your augmented types into a separate .d.ts file?
I needed to update my package to next-auth": "5.0.0-beta.4 and drizzle-adapter:"^0.3.9", and could just run it inline.
Thanks all for confirming it was me and not NextAuth.
import NextAuth from "next-auth";
import Discord from "next-auth/providers/discord";
import type { NextAuthConfig } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db } from "@/app/db";
import { pgTable } from "./db/schema";
import { env } from "@/env.mjs";
enum UserRole {
Admin = "admin",
User = "user",
Free = "free",
}
declare module "@auth/core/types" {
interface Session {
user: {
id: string;
// ...other properties
role: UserRole;
}
}
interface User {
// ...other properties
role: UserRole;
}
}
export const config = {
theme: {
logo: "https://next-auth.js.org/img/logo/logo-sm.png",
},
providers: [
Discord({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
adapter: DrizzleAdapter(db, pgTable),
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
role: session.user?.role,
},
}),
},
} satisfies NextAuthConfig;
export const { handlers, auth } = NextAuth(config);
@cexra Try using
@auth/core/typesrather than@auth/core, that worked for me.
thanks bro
I gave this a go today and no luck still unfortunately. How come this works for you guys when this is still open? https://github.com/nextauthjs/next-auth/pull/8561
So I spend quite some time debugging this, as both @auth/core & @auth/core/types doesn't work for me, and I don't think adding the /types is really necessary.
I managed to solve it by making the user optional, which make sense as the user can be undefined when user is not login.
declare module "next-auth" {
interface Session {
user?: {
id: string;
// ...other properties
role: UserRole;
}
}
}
Notice that it also works with next-auth in my case, so I believe it should also works with @auth/core & @auth/core/types.
I've tested various releases of next-auth@beta, however, I am still unable to overwrite the default values in the User interface. I saved the following into next-auth.d.ts, however, I am still getting an error when I try to assign a Number to id in authorize().
declare module "next-auth" {
interface User {
id: number;
}
interface Session {
user?: User;
}
}
declare module "@auth/core" {
interface User {
id: number;
}
}
declare module "@auth/core/types" {
interface User {
id: number;
}
}
@rvndev did you include your next-auth.d.ts in your tsconfig.json types?
I've added this snippet in my auth.config.ts and it seemed to work:
declare module "next-auth" {
interface Session {
user: {
address: string
} & User
}
interface User {
foo?: string
}
}
Check this out for more details: https://authjs.dev/getting-started/typescript
Extending the native Auth.js type + actually passing those extended values to the session is way more of a nightmare than it should be. Crazy this wasn't improved from v4 to v5, in fact, it's actually worse. Why is this issue closed?
Following the documentation perfectly on the TypeScript section throws errors and fails.
types.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: {
/** The user's postal address. */
address: string
/**
* By default, TypeScript merges new interface properties and overwrites existing ones.
* In this case, the default session user properties will be overwritten,
* with the new ones defined above. To keep the default session user properties,
* you need to add them back into the newly declared interface.
*/
} & DefaultSession["user"]
}
}
auth.ts
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [redacted],
session: {
strategy: "jwt",
maxAge: 48 * 60 * 60, // 48 hours
},
callbacks: {
session({ session, token, user }) {
// `session.user.address` is now a valid property, and will be type-checked
// in places like `useSession().data.user` or `auth().user`
return {
...session,
user: {
...session.user,
address: user.address,
},
};
},
},
});
The above example is taken directly from the documentation, and yields:
Unsafe assignment of an error typed value.eslint[@typescript-eslint/no-unsafe-assignment](https://typescript-eslint.io/rules/no-unsafe-assignment)
Property 'address' does not exist on type 'AdapterUser'.ts(2339)
This issue needs to be re-opened, at a minimum the docs need to be fixed.
Following the documentation perfectly on the TypeScript section throws errors and fails.
types.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: { /** The user's postal address. */ address: string /** * By default, TypeScript merges new interface properties and overwrites existing ones. * In this case, the default session user properties will be overwritten, * with the new ones defined above. To keep the default session user properties, * you need to add them back into the newly declared interface. */ } & DefaultSession["user"] } }auth.ts
export const { handlers, auth, signIn, signOut } = NextAuth({ providers: [redacted], session: { strategy: "jwt", maxAge: 48 * 60 * 60, // 48 hours }, callbacks: { session({ session, token, user }) { // `session.user.address` is now a valid property, and will be type-checked // in places like `useSession().data.user` or `auth().user` return { ...session, user: { ...session.user, address: user.address, }, }; }, }, });The above example is taken directly from the documentation, and yields:
Unsafe assignment of an error typed value.eslint[@typescript-eslint/no-unsafe-assignment](https://typescript-eslint.io/rules/no-unsafe-assignment) Property 'address' does not exist on type 'AdapterUser'.ts(2339)This issue needs to be re-opened, at a minimum the docs need to be fixed.
@dir To fix this just augment the module "@auth/core/adapters".
You do need to install it though (pnpm add "@auth/core" and make sure it's the latest version by checking the npm releases or on github.)
Bellow see an example:
declare module "next-auth" {
interface Session {
user: {
role: UserRoles;
id: string;
email: string;
activated: UserActivated;
} & DefaultSession["user"];
}
interface User {
role: UserRoles;
activated: UserActivated;
}
}
//For the adaptor user objcet
//Here add any other fields you have in user table that are not part of the default schema.
declare module "@auth/core/adapters" {
interface AdapterUser extends User {
role: UserRoles;
activated: UserActivated;
}
}
@ypanagidis I appreciate your help! I installed @auth/core at the latest version (0.34.1)
But... (after restarting my editor, clearing my modules folder, reinstalling, etc), I get
Invalid module name in augmentation, module '@auth/core/adapters' cannot be found.ts(2664)
⚠ Error (TS2664) |
Invalid module name in augmentation, module
cannot be found.
On a simple reproduction of your example:
declare module "@auth/core/adapters" {
interface AdapterUser extends User {
role: UserRoles;
activated: UserActivated;
}
}
This is nightmarish just to extend my session and add a couple of types. What a headache.
@ypanagidis I appreciate your help! I installed
@auth/coreat the latest version (0.34.1)But... (after restarting my editor, clearing my modules folder, reinstalling, etc), I get
Invalid module name in augmentation, module '@auth/core/adapters' cannot be found.ts(2664) ⚠ Error (TS2664) | Invalid module name in augmentation, module cannot be found.On a simple reproduction of your example:
declare module "@auth/core/adapters" { interface AdapterUser extends User { role: UserRoles; activated: UserActivated; } }This is nightmarish just to extend my session and add a couple of types. What a headache.
@dir I had the same issue in a monorepo, make sure to import anything from '@auth/core" and the error should go away! I think it's getting treeshaken
I tried everything above and I still couldn't overwrite the interfaces User and Session, any other suggestions?
package.json
{
"name": "application",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@emotion/is-prop-valid": "^1.2.2",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@mui/icons-material": "^5.15.20",
"@mui/material": "^5.15.20",
"@next/third-parties": "^14.2.4",
"@react-oauth/google": "^0.12.1",
"@svgr/webpack": "^8.1.0",
"axios": "^1.7.2",
"date-fns": "^3.6.0",
"dotenv": "^16.4.5",
"firebase": "^10.12.3",
"next": "^14.2.4",
"next-auth": "^5.0.0-beta.20",
"react": "^18",
"react-apple-signin-auth": "^1.1.0",
"react-datepicker": "^7.2.0",
"react-dom": "^18",
"react-icons": "^5.2.1",
"react-input-mask": "3.0.0-alpha.2",
"styled-components": "^6.1.11",
"superstruct": "^2.0.2",
"unstorage": "^1.10.2"
},
"devDependencies": {
"@types/next-auth": "^3.15.0",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/react-input-mask": "3.0.2",
"@types/styled-components": "^5.1.34",
"eslint": "^8",
"eslint-config-next": "14.2.4",
"typescript": "^5"
}
}
Can someone explain why when setting different types in next-auth.d.ts, I have all other types fail altogether
The file has been added to tsconfig.json
Fixes it if I move it to auth.ts are all types, but this file will be very huge anyway. I don't want to shove any more types in there.
I downgraded to stable version 4.24.7.
@ypanagidis I appreciate your help! I installed
@auth/coreat the latest version (0.34.1)But... (after restarting my editor, clearing my modules folder, reinstalling, etc), I get
Invalid module name in augmentation, module '@auth/core/adapters' cannot be found.ts(2664) ⚠ Error (TS2664) | Invalid module name in augmentation, module cannot be found.On a simple reproduction of your example:
declare module "@auth/core/adapters" { interface AdapterUser extends User { role: UserRoles; activated: UserActivated; } }This is nightmarish just to extend my session and add a couple of types. What a headache.
Have you come across a solution yet?