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
ippo
ippo•14mo ago
hmmm..... 🤔
Endgame1013
Endgame1013•14mo ago
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
ippo
ippo•14mo ago
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
Endgame1013
Endgame1013•14mo ago
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.
Anna | DevMiner
Anna | DevMiner•14mo ago
jwt > sessions
Endgame1013
Endgame1013•14mo ago
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
Anna | DevMiner•14mo ago
Anna | DevMiner
Anna | DevMiner•14mo ago
too long
ippo
ippo•14mo ago
@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 🙂
Endgame1013
Endgame1013•14mo ago
@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.
ippo
ippo•14mo ago
so what do you use in production for session based authentication?
Endgame1013
Endgame1013•14mo ago
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
Endgame1013
Endgame1013•14mo ago
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
ippo
ippo•14mo ago
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 😦 ?
Endgame1013
Endgame1013•14mo ago
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.
ippo
ippo•14mo ago
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
Endgame1013
Endgame1013•14mo ago
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.
Endgame1013
Endgame1013•14mo ago
GitHub
GitHub - nick-cheatwood7/redis-next-session
Contribute to nick-cheatwood7/redis-next-session development by creating an account on GitHub.
ippo
ippo•14mo ago
@Endgame1013 do you have a tRPC server with next-session example?
Endgame1013
Endgame1013•14mo ago
I don’t, but you should be able to reuse the same logic in a tRPC router.
ippo
ippo•14mo ago
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:
app.use(
session({
name: "COOKIE_ID",
store: new RedisStore({
client: redis,
disableTouch: true,
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 1 day
httpOnly: true,
sameSite: "lax", // reLAXed CSRF - Cross Site Request Forgery
secure: true, // cookie only works in https
domain: ".myapp.com", // cookie only works if request comes from this domain
},
saveUninitialized: false, // not every session will be stored, only modified once
secret: "very complicated string", // this string is used to sign the cookie and protect it from modifications.
resave: false, // will not save the session in store on every request, only if was modified
})
);
app.use(
session({
name: "COOKIE_ID",
store: new RedisStore({
client: redis,
disableTouch: true,
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 1 day
httpOnly: true,
sameSite: "lax", // reLAXed CSRF - Cross Site Request Forgery
secure: true, // cookie only works in https
domain: ".myapp.com", // cookie only works if request comes from this domain
},
saveUninitialized: false, // not every session will be stored, only modified once
secret: "very complicated string", // this string is used to sign the cookie and protect it from modifications.
resave: false, // will not save the session in store on every request, only if was modified
})
);
is there a next-auth equivalent to this?
Want results from more Discord servers?
Add your server
More Posts
Unknown arg `emailVerified` in data.emailVerified for type UserCreateInput.``` pprisma:query SELECT `main`.`User`.`id`, `main`.`User`.`name`, `main`.`User`.`email`, `main`.`UsFunction overloads with more than 1 parameterIm trying to overload a function that the first two parameters are string and are not optional, the Acceptable Layout ShiftIs this acceptable layout shift for a skeleton loader? Not sure how to handle the possibility of theHow does a React component require data to render in a SPAI watching Theo's video on SSR https://youtu.be/kUs-fH1k-aM?t=358 and around 5:58 he mentions that iWhy do my static files not load on first load and my links not work on first click?The logo of my navbar is a static file under the public folder. It works just fine on localhost but Should a react layout component be responsible for initial required data fetching.So I am building a dashboard for a course page. There are different routes that the course page can how to properly handle passing trpc results as parameters?I have the post data fetched like this: const {data} = api.posts.getAll.useQuery(); then I passed iHelp with s3I got the bucket: ``` show se-1 ep-1 se-2 ep-1 se-3 ep-1 ``` and I want to get thehow to use indexeddb with react query (dexie.js)im working on a project that doesnt have a hosted database, everything is done locally. im using deflexible tRPC - is it as flexible as GraphQL?airbnb and graphql had the problem to have scalable APIs that can grow, are flexible and are backwar