I'm using NextAuth in my NextJS 13 app. Initially, when the user is authenticated, session returns three parameters: name
, email
and picture
. My goal is to add an additional parameter progress
that will be updated during the user's work.
As I keep the registered user in MongoDB I use NextJS Api Route to update the user and add progress
parameter by PUT request. But the problem is despite that additional parameter was added, the session keeps returning the same three parameters: name
, email
and picture
without progress
.
The question is how to force the session to return additional parameter?
PS. At the same time I'm not sure that I'm doing it right by trying to add additional parameter via API instead of NextAuth itself 🤔
To extend the session object for NextAuth you have to modify the session callback like so:
async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.progress = user.progress;
}
return session;
},
After extending the object, you need to extend the types aswell.
Like so:
import type { DefaultSession } from "next-auth";
declare module "next-auth" {
interface Session {
user?: {
id: string;
progress: string;
} & DefaultSession["user"];
}
interface DefaultUser {
progress: string;
}
}