Extend types for session.user #2499
-
Question 💬Hello everyone, I needed to add some extra attributes to my user object in next-auth so I did this using the TypeORM adapter from next-auth. I did that by creating a user model for the database and also adding the extra attributes to the JWT token with the code below. async session(session, token: User) {
if (session?.user) {
try {
const findUser = await users.findOne({ _id: token.sub });
token.isAdmin = findUser.isAdmin;
token.isBanned = findUser.isBanned;
} catch (e) {
console.error(e);
}
session.user = { ...session.user, ...token };
}
return session;
}, However, when I try to access it in a component I get a typescript error below: How to reproduce ☕️adapter: adapter: Adapters.TypeORM.Adapter(process.env.MONGO_URI, {
models: {
//@ts-ignore
User: Models.User,
},
}), user model: import Adapters from "next-auth/adapters";
export default class User extends (<any>Adapters.TypeORM.Models.User.model) {
constructor(name, email, image, isBanned, isAdmin) {
super(name, email, image, isBanned, isAdmin);
if (!this.isBanned) this.isBanned = false;
if (!this.isAdmin) this.isAdmin = false;
}
}
export const UserSchema = {
name: "User",
target: User,
columns: {
...Adapters.TypeORM.Models.User.schema.columns,
isBanned: {
type: "boolean",
default: false,
},
isAdmin: {
type: "boolean",
default: false,
},
},
}; session callback above Contributing 🙌🏽No, I am afraid I cannot help regarding this |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I think that there should be an easy way to extend the types for session.user |
Beta Was this translation helpful? Give feedback.
-
https://next-auth.js.org/getting-started/typescript#module-augmentation |
Beta Was this translation helpful? Give feedback.
-
made a pr to relate these docs to each other: #2541 |
Beta Was this translation helpful? Give feedback.
https://next-auth.js.org/getting-started/typescript#module-augmentation