next-auth session callback not recognizing new user property

I updated the User schema in schema.prisma like this:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
stripeCustomerId String?
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
stripeCustomerId String?
}
and next-auth.d.ts
declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
stripeCustomerId: string;
} & DefaultSession["user"];
}
}
declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
stripeCustomerId: string;
} & DefaultSession["user"];
}
}
then inside [...nextauth].ts I have this:
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.stripeCustomerId = user.stripeCustomerId;
}

return session;
},
},
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.stripeCustomerId = user.stripeCustomerId;
}

return session;
},
},
The user.id works fine and it isn't complaining. but user.stripeCustomerId is... I have tried: 1. Restarting TS server. 2. npx prisma generate 3. Restart VS code Please let me know if you have stumbled upon this / know what the problem is. I am able to console.log(user.stripeCustomerId) so I know it is working.
3 Replies
amanuel
amanuelOP2y ago
I added this to my next-auth.d.ts as temporary solution, but is this correct?
interface User extends DefaultUser {
stripeCustomerId: string;
}
interface User extends DefaultUser {
stripeCustomerId: string;
}
BarisP
BarisP2y ago
I am not sure if it's because you made it optional in the prisma model but not on the type like | null or some other thing but that's how I done the thing at the time, I am open for improvement suggestions
Lopen
Lopen2y ago
most times all you need is to just restart your vscode
Want results from more Discord servers?
Add your server