The JWT callback states that: Anything you return here will be saved in the JWT and forwarded to the session callback. There you can control what should be returned to the client.. But I don't really understand what the purpose of the JWT callback is. Is it just used to create a JWT and pass the JWT to the session callback? And what is the point of the JWT that is created when I can just access the id of the user in the session in the frontend and use that to fetch data from the backend?
The contents in the image is the token passed to the session callback.
// auth.ts in project root
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
credentials: {
username: {},
password: {}
},
authorize: async (credentials) => {
return {
id: user._id.toString(),
username: user.username,
email: user.email,
};
}
})
],
callbacks: {
jwt({ token, user }) {
if (user) {
token.id = user.id
}
return token
},
session({ session, token }) {
session.user.id = token.id as string
return session
}
}
})