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 ?
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.