cookie session - express-session with tRPC
Does anybody use express-session with tRPC?
What is the best solution to use cookie sessions with tRPC?
(using redis as a storage)
23 Replies
hmmm.....
🤔
It depends on your backend. Personally, I’ve used both express session (for express backend) and fastify session (for fastify backend) with tRPC. If your backend is Next.js, I would recommend checking out the next-session package and using a custom store using Redis. Here are the docs: https://github.com/hoangvvo/next-session
GitHub
GitHub - hoangvvo/next-session: Simple promise-based session middle...
Simple promise-based session middleware for Next.js, micro, Express, and more - GitHub - hoangvvo/next-session: Simple promise-based session middleware for Next.js, micro, Express, and more
whats about next-auth?
is it possible to create session authentication with it like in express-session?
next-session is still supported?
does anybody know a repo with session authentication that uses database/redis/memory to store the id and gets the user from the database of the session for each request?
I only found JWT examples or example that use authentication servers like google and others
You can do session auth with next-auth, but only if you're using some sort of OAuth provider. It doesn't support sessions when using credential-based auth. Next-session still works. It's a pretty lightweight package, so I wouldn't expect a lot of activity on the GitHub repo.
There are not many repos out there that showcase session auth + Next.js. My guess is because Next.js is popular with the serverless community, hence the use of JWTs. If you read the next-session docs, you'll see you can write your own custom Session Store (I think the example in the repo is an in-Memory store). You can write all the logic to lookup the user from whatever DB you choose, using whatever ORM you choose. Then, you could use next-session and a custom session store using Redis to save and update the user session in Redis.
jwt > sessions
I'm personally not a fan of next-auth, as I feel it is too opinioned for my use-case. I would suggest installing next-session and giving it a spin with a custom Redis store.
@Anna | DevMiner
https://redis.com/blog/json-web-tokens-jwt-are-dangerous-for-user-sessions/
https://betterprogramming.pub/stop-using-json-web-tokens-for-authentication-use-stateful-sessions-instead-c0a803931a5d
https://gist.github.com/samsch/0d1f3d3b4745d778f78b230cf6061452
https://apibakery.com/blog/tech/no-jwt/
https://evertpot.com/jwt-is-a-bad-default/
http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/
https://developer.okta.com/blog/2017/08/17/why-jwts-suck-as-session-tokens
https://dev.to/pragativerma18/how-jwts-could-be-dangerous-and-its-alternatives-3k3j
🙂
@Endgame1013 first of all thanks for the answer
so what do you think about next-session, I mean is it still maintained?
too long
@Anna | DevMiner do not read it now, just bookmark them and give them a look if you find the time
this article is a very good summary:
http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/
take your time and give it a look if you feel ready 🙂
@ippo I personally like next-session, although I'm not actively using it in any apps actually in production. To be fair, everyone has different preferences when it comes to NPM packages, so I would suggest installing it for yourself and see if it fits well in your stack.
so what do you use in production for session based authentication?
Next.js is still pretty new to our stack at my day job. Most our apps are still on CRA/Express JS, so we're using express-session. At the point we transition to Next.js, we'll probably reach for next-session, lucia-auth, or next-auth. It all depends on what OAuth providers we will support, etc.
Here's a post I made a few weeks back when I was asking the same questions as you are: https://discordapp.com/channels/966627436387266600/1091389956695539823
You may also want to check out Lucia Auth. It just hit its 1.0 release and you can use Redis to store your user sessions. They have a pretty active Discord as well, so I'm sure others would be happy to help answer any questions you may have. https://lucia-auth.com/
Lucia Documentation
Lucia
I am not sure but next-auth feels wrong
can not say why :/
@Endgame1013 can you elaborate on OAuth and session based authentication?
OAuth is a protocol for token based authentication
at the moment I can not see how next-auth and OAuth with session work together 😦 ?
I’m not going to be as much help as reading the docs. In short, next-auth is only going to allow you to use JWTs, unless you use an OAuth provider to authenticate users. If you’re wanting a way to save user sessions to a db like Redis, regardless of how the user is authenticated (credentials, OAuth, magic link, etc.), you’re going to want to reach for another library besides next-auth.
This all ties back to my original point that I think next-auth is too opinionated.
long story short: there is a provider that offers you session authentication with next-auth, but you never tested that, right?
side comment:
is it me or is it normal that I feel alone using session authentication?
it feels like you are stupid if you do not use JWT and there is almost no native session solution or support
I sent a link a couple messages up that links to a repo I made using next-session with a custom redis store. You may want to check that out and clone the repo for yourself.
And the reason JWTs are so popular with Next.js is because they are more portable than sessions and work better with serverless deployments.
GitHub
GitHub - nick-cheatwood7/redis-next-session
Contribute to nick-cheatwood7/redis-next-session development by creating an account on GitHub.
@Endgame1013
do you have a tRPC server with next-session example?
I don’t, but you should be able to reuse the same logic in a tRPC router.
hmmm...
will give it a try
will need your help, but will try it alone before 🙂
this is how I setup my cookie with express-session:
is there a next-auth equivalent to this?
Options | NextAuth.js
Environment Variables