How does remember me works in Next Auth #9255
-
Hey everyone! I'm in the process of developing a full-stack app using Next.js for the front-end and a Spring Boot-based external API. Good news – I've successfully integrated NextAuth for authentication with my external API. Now, here's my query: Does NextAuth have any built-in features related to checkbox functionality? Specifically, I'm looking to implement a behavior where the session is persisted only if the user checks the "Remember Me" option. I'm quite new to Next.js and still in the learning phase, so any guidance or insights, especially with regards to this checkbox feature, would be incredibly helpful. If you know of any relevant documentation, please point me in the right direction! Thanks a bunch! Code:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
@jpcchaves have you found any solution for implementing the 'remember-me' feature with next-auth? |
Beta Was this translation helpful? Give feedback.
-
I have forged a solution for this
|
Beta Was this translation helpful? Give feedback.
-
If you create two new field in the token you can build a workaround for this.
Then in the backend (auth.ts), I am handling it like this:
Basically I added two fields to the token (maxAge and originalExpires). You just set the "maxAge" parameter of NextAuth to the maximum time a session can be valid and then check if your own session expiry date (originalExpires) is valid on every request. If it's not valid anymore, I redirect them to "/auth/logout" which then makes use of the "signOut" function to nullify the current session. If you just delete the cookie, someone technically could just copy the session, change the originalExpires date and reuse it. This way the session gets completely nullified and useless. To make sure that nobody can use the session with an expired originaleExpires field, you have to authenticate every request in your middleware.ts.
|
Beta Was this translation helpful? Give feedback.
I have forged a solution for this
authOptions.ts