-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add cookie-based last login tracking for NextAuth #391
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
base: main
Are you sure you want to change the base?
feat: add cookie-based last login tracking for NextAuth #391
Conversation
Implements a cookie-based solution to track the last login timestamp and OAuth provider used, similar to Better Auth's last-login-method plugin but adapted for NextAuth. Changes: - Modified NextAuth handler to intercept OAuth callbacks and set a 'last-login' cookie - Cookie contains JSON with timestamp and provider name (google, github, discord, linkedin, azure-ad) - Cookie is HttpOnly, SameSite=Lax, with 1-year expiration - Added utility functions in src/utils/last-login.ts for reading and formatting the cookie data - Includes helper functions for client-side and server-side cookie access - Added comprehensive documentation in LAST_LOGIN_USAGE.md with usage examples This implementation uses cookies instead of database storage to minimize overhead and complexity.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile OverviewGreptile SummaryImplements cookie-based tracking of last login timestamp and OAuth provider by intercepting NextAuth callbacks and setting a custom cookie. However, the implementation has a critical flaw: the cookie is marked as HttpOnly (for security), which prevents client-side JavaScript from accessing it via Major issues:
Missing from PR description:
The feature needs to either remove the HttpOnly flag (reducing security) or update the documentation to clarify that client-side access is not possible and remove the non-functional Confidence Score: 1/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant Browser
participant NextAuthHandler
participant OAuthProvider
participant LastLoginCookie
User->>Browser: Initiate OAuth login
Browser->>NextAuthHandler: Start auth flow
NextAuthHandler->>OAuthProvider: Redirect for authentication
OAuthProvider->>User: Display consent screen
User->>OAuthProvider: Approve access
OAuthProvider->>NextAuthHandler: Callback with credentials
Note over NextAuthHandler: Extract provider from callback URL
NextAuthHandler->>NextAuthHandler: Override setHeader method
NextAuthHandler->>LastLoginCookie: Set HttpOnly cookie with timestamp and provider
NextAuthHandler->>Browser: Redirect with session and cookie
alt Client-side cookie access
Browser->>LastLoginCookie: Attempt document.cookie read
LastLoginCookie-->>Browser: Blocked by HttpOnly flag
end
alt Server-side cookie access
Browser->>NextAuthHandler: Send request with cookies
NextAuthHandler->>LastLoginCookie: Read from req.cookies
LastLoginCookie-->>NextAuthHandler: Return timestamp and provider data
NextAuthHandler->>Browser: Send response with login info
end
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 4 comments
|
|
||
| ## Security Notes | ||
|
|
||
| - The cookie is **HttpOnly**, meaning it cannot be accessed via JavaScript on the client side for security purposes. Use the provided utility functions instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: HttpOnly cookies cannot be read via document.cookie in JavaScript, which contradicts the client-side function getLastLoginInfo() provided in the docs. Either remove HttpOnly flag from the cookie implementation, or document that client-side access won't work with HttpOnly enabled.
Prompt To Fix With AI
This is a comment left during a code review.
Path: LAST_LOGIN_USAGE.md
Line: 144:144
Comment:
**logic:** HttpOnly cookies cannot be read via `document.cookie` in JavaScript, which contradicts the client-side function `getLastLoginInfo()` provided in the docs. Either remove HttpOnly flag from the cookie implementation, or document that client-side access won't work with HttpOnly enabled.
How can I resolve this? If you propose a fix, please make it concise.| signIn({ account }) { | ||
| // This callback runs on successful sign-in | ||
| // We'll set the cookie in the handler below | ||
| return true; | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Empty callback serves no purpose and should be removed.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/pages/api/auth/[...nextauth].ts
Line: 24:28
Comment:
**style:** Empty callback serves no purpose and should be removed.
How can I resolve this? If you propose a fix, please make it concise.| events: { | ||
| async signIn(message) { | ||
| // Store provider and timestamp for cookie setting in handler | ||
| // The actual cookie is set in the custom handler below | ||
| }, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Empty event handler serves no purpose and should be removed.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/pages/api/auth/[...nextauth].ts
Line: 60:65
Comment:
**style:** Empty event handler serves no purpose and should be removed.
How can I resolve this? If you propose a fix, please make it concise.| export function getLastLoginInfo(): LastLoginInfo | null { | ||
| if (typeof window === "undefined") { | ||
| console.warn("getLastLoginInfo can only be called on the client side"); | ||
| return null; | ||
| } | ||
|
|
||
| const cookies = document.cookie.split(";"); | ||
| const lastLoginCookie = cookies.find((cookie) => | ||
| cookie.trim().startsWith("last-login=") | ||
| ); | ||
|
|
||
| if (!lastLoginCookie) return null; | ||
|
|
||
| const cookieValue = lastLoginCookie.split("=")[1]; | ||
| return parseLastLoginCookie(cookieValue); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: getLastLoginInfo() won't work if cookie has HttpOnly flag (which it does in [...nextauth].ts:108). HttpOnly prevents client-side JavaScript from accessing cookies via document.cookie.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/utils/last-login.ts
Line: 41:56
Comment:
**logic:** `getLastLoginInfo()` won't work if cookie has HttpOnly flag (which it does in `[...nextauth].ts:108`). HttpOnly prevents client-side JavaScript from accessing cookies via `document.cookie`.
How can I resolve this? If you propose a fix, please make it concise.
Implements a cookie-based solution to track the last login timestamp and OAuth provider used, similar to Better Auth's last-login-method plugin but adapted for NextAuth.
Changes:
This implementation uses cookies instead of database storage to minimize overhead and complexity.