express-session for tRPC
I am using express-session for all my servers to create session authentication.
Is there something for tRPC that you can recommend, to get session authentication?
7 Replies
does anyone know a repo, that uses cookie authentication where the session-id is stored in a database/redis/memory and where on every request the user is queried and store in the req object?
you can send the auth cookie or similar with the request in the headers
(https://trpc.io/docs/client/setup)
then define a middleware that uses the auth to find a matching session (in your redis etc.)
(https://trpc.io/docs/server/middlewares)
@tomheaton
but where do you set the cookie properties?
the max age, the signature secret, its behavior and so on?
You can use express session as normal with trpc No magic Here. You dont need anything specilal just for trpc. Trpc is based on the req/res from express so you have access to the req.session within trpc routes.
this is how I setup my session with express-session:
Is there an equivalent for next-auth?
You need an adapter with next-auth. I see u use redis with express. Then either use up-stash adapter for redis or you need to roll out your own adapter for redis. There is custom adapter for redis on next-auth issues or discussions as I was in need for one too. You have to look for it
https://next-auth.js.org/adapters
https://authjs.dev/reference/adapter/upstash-redis
https://authjs.dev/guides/adapters/using-a-database-adapter
https://authjs.dev/concepts/faq#databases
https://authjs.dev/reference/adapters
vercel just announced
vercel-kv
🙌