Skip to content

73 backend remember me #90

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

Merged
merged 6 commits into from
Apr 1, 2025
Merged

73 backend remember me #90

merged 6 commits into from
Apr 1, 2025

Conversation

ManuP6789
Copy link
Collaborator

Description

Added remember me functionality for login. I decided not to use the cookies library since it would have required for us to create our own cookie and handle the remember me sessions by ourselves and logout the user after the set time period. I think it was better to use the JTW token from nextauth (which we already use). I had to modify the maxAge setting which handles how long until an idle session expires and is no longer valid. Since you cannot directly edit the change the maxAge on every reload of the session in options.ts. I had to dynamically pass in a dynamicMaxAge from router.ts to options.ts. It compares the current time with the expiration time (30 days or 1 day) to check how long the MaxAge has until it expires the session.

  • By doing this we ensure that we do not make mistakes in our own cookie implementation.
  • It follows secure standard practices that nextAuth already has with JTW Tokens.
  • It ensures consistency between the session.expires, maxAge, and token.exp. (It can be retrieved from anywhere in the page to see how long the session is going to last, if needed)

Issues

#73

Screenshots

N/A

Test

In order to test, I left console.logs that are useful for testing the session expiration times and that every value that holds the expiration date or expiration time is consistent in the JWT token, session, and nextAuth options.ts maxAge.

  1. In options.ts, line 85: change -> token.expiresIn = token.rememberMe ? 30 * 24 * 60 * 60 : 24 * 60 * 60; to token.expiresIn = token.rememberMe ? 90 : 20; (seconds, left is Remember me ON)
  2. In route.ts, line 16: change -> maxAge = token.rememberMe ? 30 * 24 * 60 * 60 : 24 * 60 * 60; to maxAge = token.rememberMe ? 90 : 20;
  3. Login with remember me on and look at your console to see that the console log shows maxAge to 90 or 89 (depending on delay)
  4. Close the page and open it again and should see the maxAge go down. Additionally look at the session number, jwt callback number, and exp in the JTW token and they should all be the same. That is the expiration date!
  5. Stay on the page until the 90s pass and you will be redirected to the login page.
  6. Follow steps 3-5 with rememberMe OFF, it should now start at 20s.

I will delete the console.logs once the pr is reviewd!

Possible Downsides

Since we are relying entirely on the nextAuth JWT token, if there is a fixed default behavior that we are unaware, it may cause issues. So far I have not seen any, but it may be hard to test in the future.

Additional Documentations

@ManuP6789 ManuP6789 requested a review from wkim10 March 17, 2025 13:16
@ManuP6789 ManuP6789 self-assigned this Mar 17, 2025
@ManuP6789 ManuP6789 linked an issue Mar 17, 2025 that may be closed by this pull request
@@ -0,0 +1,13 @@
import Cookies from "js-cookie";

export const setRememberMeCookie = (token: string, rememberMe: boolean) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem like this function is being used, were you planning on using it for something or is it okay to remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I forgot to remove it. Thanks 🙏

Copy link
Collaborator

@wkim10 wkim10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work, just added a few fields to fix the token types. Feel free to merge after you remove all the console logs!

@ManuP6789 ManuP6789 merged commit bed8bdb into main Apr 1, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Backend] Remember Me
2 participants