Reador
Explore posts from serversTTCTheo's Typesafe Cult
•Created by Reador on 11/21/2024 in #questions
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.
6 replies
Express + tRPC: handle express errors from client
I currently have Express as backend and react on frontend.
I want to migrate gradually to tRPC + react query.
In order to do that I want to use the tRPC express middleware. I have tons of express middleware that I want to continue using for now, as well as error handlers.
The problem is that the tRPC client (trpc react query) does not recognize express errors like custom session timeout, 401, etc, which is normal, so I get the "Unable to transform response from server".
But I would like to know if there is an easy way to format express errors easily to tRPC errors, or if its possible to catch the error before tRPC client tries to transform it, so that I can handle it myself.
17 replies