NextAuth getServerSession used multiple time

I wonder what is the best way to know if your user is authenticated in multiple places in Next App router using NextAuth.

Super classic usecase (I would think): I need to know if Im authenticated both from my header (RSC) and page (RSC). If I use getServerSession two times, one in each RSC, my DB will be fetched two times, clearly feels overkill for a single server request overall.

What would be the recommended way then ?

  • Calling getServerSession from the root layout and passing it down everywhere ? Seems not convenient and quite bad DX.
  • Using a client provider at the root and using client useSession everytime I need it ? Seems like not the RSC way of doing it.
  • Creating my custom context where I fetch if Im authenticated in server once and share it across the app on client ?
  • Caching the session on server but not secure.
There must be a easier way right ? My usecase seems quite common but Im a noob in both NextJS and NextAuth, maybe Im missing something.
Was this page helpful?