-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JWT maxAge: what is the default value? #796
Comments
Good questions!
The default value for The default session It looks like we have covered this well in the I honestly forget why
Where it gets complicated is the By default, a Next.js Single Page App using NextAuth.js will load session once and on subsequent page transitions, the However, the app wide session state WILL be updated in the SPA if the window loses/regains focus or if the user logs out in another window in the same browser (it uses event hooks to do this) OR anytime you call The The As per the docs above, in most cases, if your
Using something like 30 days (or even longer, you might want 90 days, or a year) is fine in almost all cases. The only reason folks typically worry about shorter session times with JWT is that, unlike a database session, you can't just delete a user's session entry from the database to expire their session and force them to be signed out as the token is stored on their side. However, you can still prevent them from preforming an action by checking the token and rejecting it in your own API routes, even if they have a JWT but you have since deleted / blocked their account on your servers. Working out if this is a thing you need to worry about probably depends on the specifics of the particular app and your use cases. Thinking about how you want to handle abuse / bad actors / deleted users / accounts not in good standing (e.g. expires) on your platform will help you figure out if you need to worry about this and what the easiest way for you to handle those situations is. A typical example might be, if it was a forum you might check the user in the JWT against a database of blocked / suspended / temporarily banned accounts and prevent users who were in that list from posting. This downside is the effectively the crux of the tradeoff for using JSON Web Tokens - it can be faster for users and cheaper at scale, but means handling some flows is harder. |
Oops, sorry didn't mean to close this already! If you have any follow up questions or I've written nonsense (it's a big wall of text so I might have in places....) please feel free to re-open so I don't miss them. |
Thanks @iaincollins, that's great answers 👍 |
Great explanation @iaincollins. However for security perspectives and to avoid having a blocked/suspended central list, a short living access token could be used to protect your routes. Instead of using the session jwt, you could generate a short living access token jwt yourself and use it in your backend/api routes. The session jwt from next-auth will be then used as a "refresh" token. One possible implementation is this:
and in the _app.js provider set the A good article to read is: https://hasura.io/blog/best-practices-of-using-jwt-with-graphql/ |
@iaincollins could you please help me? |
Thanks @iaincollins for this excellent explanation. |
Your question
In the session option documentation, the default value for maxAge seems to be 30 days. Is that also true if session.jwt is set to true?
I am wondering because in the FAQ there is this statement:
"You cannot as easily expire a JSON Web Token [...] Shorter session expiry times are used when using JSON Web Tokens as session tokens to allow sessions to be invalidated sooner and simplify this problem."
Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
The text was updated successfully, but these errors were encountered: