-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Next Auth V5 Augment Typescript User Interface doesnt work #9253
Comments
@cexra Try using |
yes the solution works but only partially, the solution solves the problem on jwt and session callback. but why |
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: 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 |
I needed to update my package to 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); |
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? #8561 |
So I spend quite some time debugging this, as both I managed to solve it by making the
Notice that it also works with |
I've tested various releases of
|
@rvndev did you include your I've added this snippet in my 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 |
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
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
Does the docs page already exist? Please link to it.
https://authjs.dev/getting-started/typescript?frameworks=next
The text was updated successfully, but these errors were encountered: