feat: loginview should honor nextauth callbackurl from the query string (middleware-friendly) (#955)#959
Merged
Conversation
…ng (middleware-friendly) (#955) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
525eca5 to
fc089d8
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the authentication redirect flow to honor NextAuth middleware’s ?callbackUrl= query parameter (with same-origin guarding) so users return directly to the originally requested route after login/session settle.
Changes:
- Added
getQueryCallbackUrlhelper to safely extract a same-origin callback path fromrouter.query.callbackUrl. - Updated
useNextAuthServiceanduseSessionActiveto prefer the query callbackUrl over route-history fallback. - Added/expanded Jest coverage for helper behavior and both redirect call sites (including open-redirect rejection cases).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/hooks/authentication/session/useSessionActive.ts | Prefer query callbackUrl for post-settle redirect; adds guarded getQueryCallbackUrl helper. |
| src/nextauth/hooks/useNextAuthService.ts | Prefer guarded query callbackUrl when calling service.login. |
| tests/getQueryCallbackUrl.test.ts | Adds unit tests for getQueryCallbackUrl across safe/unsafe inputs. |
| tests/useNextAuthService.test.ts | Adds hook tests to verify query preference and open-redirect guard for login. |
| tests/useSessionActive.test.ts | Extends redirect tests to cover query preference and guard behavior. |
) Addresses copilot review feedback on PR #959: Router.push and other module-level jest.fn() mocks weren't being cleared between tests, which made assertions order-dependent. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
NoopDog
approved these changes
Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getQueryCallbackUrlhelper that extracts a same-origin path fromrouter.query.callbackUrl, rejecting absolute URLs, protocol-relative (//evil.com/x), and non-/-prefixed values.useNextAuthService.onLoginanduseSessionActive's post-settleRouter.pushnow prefer the query callbackUrl over the existing route-history fallback, with the same-origin guard in place. Aligns the library with the standard NextAuth + middleware contract.useSessionActive's effect intentionally capturesqueryCallbackUrlonce at first settle (excluded from deps with an explanatory comment) so a later URL change doesn't re-trigger a redirect.Closes #955
Back-compat
?callbackUrl=) see identical behavior to before — the query is empty so the route-history fallback runs.?inactivityTimeout=true) is unaffected.Security note
The new helper deliberately blocks absolute and protocol-relative URLs so that
Router.push(which does NOT enforce same-origin) cannot be used as an open redirector via a crafted/login?callbackUrl=https://evil.examplelink. NextAuth'ssignInhas its own same-origin guard, but consumers that override theredirectcallback would lose it — this helper protects both call sites.Test plan
tests/getQueryCallbackUrl.test.ts— 10 cases (string, string[], undefined, empty, absolute URL, protocol-relative,javascript:, query/fragment preservation).tests/useNextAuthService.test.ts— new file covering the sign-in path's query-preference + open-redirect rejection.tests/useSessionActive.test.ts— extended with the query-preference branch + open-redirect rejection.npm run lintclean.npm run check-formatclean.npx tscclean.npm test— 469/469 pass./).🤖 Generated with Claude Code