Why does NextAuth use "JWT sessions" by default? #2642
-
Question 💬I love this library, I use it daily, and it's been great. I don't use JWTs, but I still am curious of why this feature exists, since NextAuth generates its own JWT, and it stores the OAuth token from the provider(s) you sign in from. How to reproduce ☕️Some material I've found that explains more about what I'm talking about: Contributing 🙌🏽No, I am afraid I cannot help regarding this |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
I won't have the perfect answer here since I am not the original creator, but mostly it comes with the advantage of not having to deal with a database. There are drawbacks as well as advantages, and we have a F.A.Q section summing things up better I probably could: https://next-auth.js.org/faq#json-web-tokens I hope that helps. That said, we do offer "traditional" database persisted sessions, if you need/want that. |
Beta Was this translation helpful? Give feedback.
-
My bad, should have made it a discussion. Forgot that those exist ^^ |
Beta Was this translation helpful? Give feedback.
-
I would second what @balazsorban44 has said. The short answer is that a JWT allows for cheap, fast and highly scalable authentication, because you don't need to maintain a list of all active sessions server side (e.g. in a database, on a file store) and it can be convenient in loosely distributed systems, where different products reading a session are managed by different teams or where different endpoints may be handled by de-coupled code (e.g. serverless functions) and standardized tooling and good documentation is available for it. There are other ways of designing proprietary solutions that deliver similar benefits (even without some of the drawbacks), but you need to be at the scale of several million concurrently active sessions with strict sub-second latency before that ought to be an issue. The main tradeoffs to using JWT for sessions include; cookie size is very limited, it increases data traffic slightly (though typically only relevant on mobile devices in limited bandwidth scenarios) and it comes with some conceptual complexity (e.g. reading, signing and how you handle expiring sessions). If using a JWT as a session token it should be stored as a cookie rather than somewhere like local storage to avoid session hijacking by third party scripts or from exploitation of cross site scripting attacks - and if you want to support clients that do not have JavaScript, or in the event there is a compatibility problem executing client side code (the more common anti-pattern here is to use local storage in an SPA because "it's easier" to store them that way, in practice that's true but it can be a security risk and is a pattern than can lead to interoperability issues). |
Beta Was this translation helpful? Give feedback.
I would second what @balazsorban44 has said.
The short answer is that a JWT allows for cheap, fast and highly scalable authentication, because you don't need to maintain a list of all active sessions server side (e.g. in a database, on a file store) and it can be convenient in loosely distributed systems, where different products reading a session are managed by different teams or where different endpoints may be handled by de-coupled code (e.g. serverless functions) and standardi…