Below follows a couple of related questions, with regards to Auth.js (formerly NextAuth.js) in Next.js.
Background:
I am using the jwt session strategy, but would still like to store more information about a user on session.user
, than what is saved on the jwt by default (default: name, email, image). For instance, I would like to add a role and and the organization they belong to. Following the docs, I have done module augmentation to extend the types of the jwt and session.user
.
I am guessing it is normal to want to store more info about a user than just the name, email, and image that Auth.js has by default.
Goal:
I have read the section on Extending the Session in the docs, but it only explains how to get the extra session data if it comes from the provider (i.e. Google). In my case, I would like to do a lookup in a database, in order to augment session.user
.
Main Question:
How can the above goal be achieved? Do I just do the database call inside the provider’s profile callback, or inside the jwt callback?
Additional Related Questions:
-
Is it bad practice to do such database calls in the middleware?
-
Since I am anyways calling the database on login/logout, am I loosing the leanness/scalability benefits of the jwt session strategy, and might just as well use the database session strategy directly?
-
How do I persist the data (e.g. role and organization) in the database when the user logs in for the first time? Do I add additional database calls to the middleware and if so, where?