How can I access the entire user model and not just id, name, email and image?
I want to access the entire model in the session, when I add the properties how can I automatically pull the values from the db?
5 Replies
if you're using prisma, it's just following the next-auth module augmentation documentation, I think the same does not apply for drizzle because of the adapter I'm afraid
Okay, I'll take a look today
U need to add a few things:
1. In base directory make a next-auth.d.ts with the following code(change it depending on what u need to add):
import { User } from "next-auth"
import { JWT } from "next-auth/jwt"
type UserId = string
declare module "next-auth/jwt" {
interface JWT {
id: UserId,
dob: string
}
}
declare module "next-auth" {
interface Session {
user: User & {
id: UserId;
dob: string;
}
}
}
2. In your authoptions add this(change depending upon your needs):
callbacks: {
async session({ token, session }) {
if (token) {
session.user.id = token.id;
session.user.name = token.name;
session.user.email = token.email;
session.user.dob = token.dob;
session.user.image = token.picture;
}
return session;
},
async jwt({ token, user }) {
const result = await findUser(token.email!);
if (!result) {
if (user) {
token.id = user?.id;
}
return token;
}
return {
id: result.id,
name: result.name,
email: result.email,
picture: result.image,
dob: result.dob,
};
},
},
In the above code, just to make it clear, I added an id and dob in the session.
my authoptions file is structured like this, should I rewrite it with the return options?
Yes i think u should
U also need to add this in authoptions:
session: {
strategy: "jwt",
},