jwt vs cookies in next-auth

we recommend using sessions in t3-app but i don't know why. theo says to use sessions but what are the exact reasons one would pick one over the other while using next-auth?
93 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
meant cookies]
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
dude i really dk the technicalities lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
i mean whatever the strategies are in next-auth one is jwt, other is the thing we use in ct3a no
dan
dan3y ago
jwt vs database sessions?
Neto
Neto3y ago
next auth is using cookies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
jwt is just the encoding cookie is the way of transport
nexxel
nexxelOP3y ago
yes this
Neto
Neto3y ago
oh
nexxel
nexxelOP3y ago
oh
Neto
Neto3y ago
adonis uses database sessions a smaller token and such
nexxel
nexxelOP3y ago
but is one way better than the other?
Neto
Neto3y ago
but you hammer the database with requests to decode
nexxel
nexxelOP3y ago
a friend of mine is using jwt because db sessions are slow for them and cookies are sent on every req this was their explanation
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
that makes sense
Neto
Neto3y ago
plus if you add db lookup if for some reason the db is down auth is down as a whole
nexxel
nexxelOP3y ago
so it generally results in a faster client side app?
dan
dan3y ago
if db is down in most cases the app wont work.
nexxel
nexxelOP3y ago
that makes sense
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
dan
dan3y ago
or atleast a lot of the functionality probably doesnt work.
Neto
Neto3y ago
indexing if fine for the case too plus its easier to invalidate
nexxel
nexxelOP3y ago
also is this a thing?
Neto
Neto3y ago
because you just purge poohheh
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
i think refreshing the jwt token in next-auth is also manual right?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
ohhh does that have any impact on performance?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cornflour
cornflour3y ago
technically probably since you check session with a db query but dont need to for jwt
nexxel
nexxelOP3y ago
that makes sense
dan
dan3y ago
if doing a db lookup is slow then adding caching would help with that. but I think for it to be noticeable you need a lot of traffic
Neto
Neto3y ago
if the lookup is an issue
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
you can add a redis or memcached
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
it is a kv for the sort token:some-payload
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
adonis as example allow you to do both
dan
dan3y ago
oh yea that reminds me, you cant invalidate a jwt token. so signing out technically isnt possible and invalidating sessions also isnt possible.
nexxel
nexxelOP3y ago
i should read up more on this thanks for the info! super helpful for noobs like me
Neto
Neto3y ago
poohheh
dan
dan3y ago
no way you're a noob :3
nexxel
nexxelOP3y ago
noob at auth* :)
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
upstash
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
Upstash Redis | NextAuth.js
To use this Adapter, you need to install @upstash/redis and @next-auth/upstash-redis-adapter package:
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
upstash is a provider same for railway
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
the speed difference would be on redis location
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
not only that upstash have a redis serverless client
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
connection pool
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
with db lookup you dont need to store the user info on the token create a row on the database, encrypt the row id even if you decrypt the key, you cant access any decent info
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
user if or token id?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Neto
Neto3y ago
its kind of ok but with getting the user profile you expose either way lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
naz6352
naz63523y ago
A bit late to the party but I found this cool flowchart that describes some problems with using JWT for sessions http://cryto.net/~joepie91/blog/attachments/jwt-flowchart.png
dan
dan3y ago
I am saving that because that I so useful. Explains shit better than I could lol
naz6352
naz63523y ago
Found the blog with that flowchart on the prisma-session-store readme if you want to read the rest of it (there's a couple other links mentioned in the readme that are pretty good reads too) https://github.com/kleydon/prisma-session-store
GitHub
GitHub - kleydon/prisma-session-store: Express session store for Pr...
Express session store for Prisma. Contribute to kleydon/prisma-session-store development by creating an account on GitHub.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
i saw this a few days back looks cool! will try it yeah database sessions seem much more straightforward than jwts
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
very cool don't think there's a solid start adapter yet tho are there any plans for that?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
i can help in contribute docs, im guessing it would be similar to the next or astro api
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
nexxel
nexxelOP3y ago
oh yeah focus on exams!
Leonidas
Leonidas3y ago
Conceptually cookies and local storage are the same (data stored clientside).
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
needmorewood
needmorewood3y ago
An http only cookie is quite different U can't read it from the application
Leonidas
Leonidas3y ago
https://stytch.com/blog/jwts-vs-sessions-which-is-right-for-you/ to understand the difference between jwt auth and session auth i can recommend this article
Lydia Gorham
Stytch
JWTs vs. sessions: which authentication approach is right for you?
Your application just received a login request, and the credentials passed successfully prove the identity of a user in your system. Wonderful, you have a high degree of confidence in who this user is and what they should be able to access! …but wait, what happens on the next API call where they don’t include […]
needmorewood
needmorewood3y ago
The browser can send it along with requests and it's unreadable from inside I tried understanding the point of jwts but opted for a userinfo endpoint to get any data I needed about the user instead
Leonidas
Leonidas3y ago
Exactly, nowadays u can even declare cookies as https only additionally - which makes them the best storage for such access tokens. But at the end that is an implementation detail. For understanding the different auth concept this is not relevant
dan
dan3y ago
when i heard about jwts 2 years ago i didn’t really see a good use case for them for 95% of applications
needmorewood
needmorewood3y ago
Fair
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
needmorewood
needmorewood3y ago
Many people especially here say that there is no security difference all the time So I'm just in auto mode when I see that line
Leonidas
Leonidas3y ago
Yeah i agree. The only real advantage they have is performance wise and their stateless nature making them easier for cross microservice communication. But they come with a pretty big disadvantage for real life use cases where fine grained control of user access is necessary
needmorewood
needmorewood3y ago
If u have no dedicated backend then Yea probably, but if you have your own backend and an spa should just do cookie stuff
IceAge2OnDVD
IceAge2OnDVD2y ago
Have you got any up-to-date documentation on how I could go about adopting lucia in my trpc/ct3a stack? I've got myself into a bit of a messy state trying to get lucia integrated
Want results from more Discord servers?
Add your server