Augment user type in JWT callback

I am trying to get access to user.role here, but no matter how I alter the next-auth.d.ts, it doesn't seem to change this user object.
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
// token.role = user.
}
return token;
},
session: ({ session, token }) => {
if (session.user) {
session.id = token.id;
session.user.role = token.role;
}
return session;
},
},
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
// token.role = user.
}
return token;
},
session: ({ session, token }) => {
if (session.user) {
session.id = token.id;
session.user.role = token.role;
}
return session;
},
},
Here is my next-auth.d.ts
import { DefaultSession } from "next-auth";

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
id: string;
user?: {
id: string;
sponsorId?: string;
role: string;
} & DefaultSession["user"];
}
}

declare module "next-auth/jwt" {
interface JWT {
id: string;
role: string;
sponsorId?: string;
}
}
import { DefaultSession } from "next-auth";

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
id: string;
user?: {
id: string;
sponsorId?: string;
role: string;
} & DefaultSession["user"];
}
}

declare module "next-auth/jwt" {
interface JWT {
id: string;
role: string;
sponsorId?: string;
}
}
3 Replies
Jakesdoc
Jakesdoc2y ago
I do have access to those values, they just aren't typed properly. This works, I just don't want to have to write as string
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
token.role = user.role as string;
token.sponsorId = user.sponsorId as string;
}
return token;
},
session: ({ session, token }) => {
if (session.user) {
session.id = token.id;
session.user.role = token.role;
session.user.sponsorId = token.sponsorId;
}
return session;
},
},
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
token.role = user.role as string;
token.sponsorId = user.sponsorId as string;
}
return token;
},
session: ({ session, token }) => {
if (session.user) {
session.id = token.id;
session.user.role = token.role;
session.user.sponsorId = token.sponsorId;
}
return session;
},
},
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Jakesdoc
Jakesdoc2y ago
That seems to have fixed it thank you!
Want results from more Discord servers?
Add your server