Skip to content

refactor(web): move NextAuth sign-in config and landing redirect out of lib/user/server - #6894

Merged
chrarnoldus merged 4 commits into
mainfrom
refactor/web-shared-prep
Sep 30, 2026
Merged

chrarnoldus merged 4 commits into
mainfrom
refactor/web-shared-prep

Conversation

@chrarnoldus

@chrarnoldus chrarnoldus commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR moves the NextAuth sign-in configuration and the post-sign-in landing logic out of lib/user/server. After this, getUserFromAuth and the other request-authentication functions no longer import the sign-in flows.

  • Moves getProfileRedirectPath unchanged to lib/user/profile-redirect-path, together with its personal-account fallback.
  • Moves the NextAuth sign-in configuration unchanged to lib/user/next-auth-options. That covers authOptions, nextAuthHttpHandler, the providers, the signIn and jwt callbacks, the profile parsers and TurnstileJwtPayload.
  • Adds sessionAuthOptions to lib/user/server and uses it for session reads (getUserFromSession and the session path of getUserFromAuth). It holds the secret, logger, pages, debug flag, the session callback, and a jwt callback that returns the token. authOptions extends it with the cookies, providers and sign-in callbacks.
  • Updates importers and Jest mocks to the new modules. It also adds a test that sessionAuthOptions and authOptions read sessions with the same configuration.
  • Makes the PostHog mock in the native token route test reuse one capture mock. Jest also runs that mock's setup while it builds the automatic @/lib/user mock, which now happens after the route has created its PostHog client.

Verification

Tests are left to CI. No manual checks are listed.

Visual Changes

N/A

Reviewer Notes

  • Reading a session with sessionAuthOptions behaves like reading it with authOptions. getServerSession replaces the providers with [], and a session read calls the jwt callback without a trigger. The full callback already returns the token unchanged in that case (if (!trigger) return token;).
  • The moves are verbatim. Use git show --color-moved=dimmed-zebra <commit> to check them; GitHub does not highlight moved blocks.
  • Context: this is the last prep step for [AISAGE-502] feat(ai-gateway): add a standalone ai-gateway app serving /api/v1 from a web-shared package #6806. It keeps the sign-in flows out of the code shared with the AI gateway app.

@chrarnoldus chrarnoldus self-assigned this Sep 29, 2026
@kilo-code-bot

kilo-code-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

The PR branch was rebased onto the current main (a49cceb), but its changed files are byte-identical to the previously reviewed commit 40b9c7d; there are no new changed lines in this run and no new issues.

Files Reviewed (0 changed since last review)
  • Incremental diff 40b9c7d..fb70b48 is empty for all 17 PR paths (git diff with literal pathspecs).
  • The NextAuth sign-in split and getProfileRedirectPath move were reviewed at 5336c50; the PostHog mock fix at 40b9c7d.

Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

Previous Review Summaries (2 snapshots, latest commit 40b9c7d)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 40b9c7d)

Status: No Issues Found | Recommendation: Merge

Executive Summary

Incremental review of the single new commit (test-only change to the native token route test) found no issues; the ??= reuse of the PostHog capture mock correctly keeps one mock instance across the automatic @/lib/user mock factory runs.

Files Reviewed (1 file, incremental)
  • apps/web/src/app/api/auth/native/token/route.test.ts

Previous review (commit 5336c50)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (127 files)
  • apps/web/src/app/(app)/organizations/[id]/sub-organizations/distribute-funds/parseDollarInput.ts
  • apps/web/src/app/admin/api/backfills/normalized-email/route.ts
  • apps/web/src/app/admin/api/organizations/[id]/consume-credits/route.ts
  • apps/web/src/app/admin/api/users/[id]/dev/insert-usage-record/route.ts
  • apps/web/src/app/admin/api/users/[id]/kill-balance-cache/route.ts
  • apps/web/src/app/admin/api/users/[id]/referrals/route.ts
  • apps/web/src/app/admin/api/users/gdpr-removal/route.test.ts
  • apps/web/src/app/admin/api/users/gdpr-removal/route.ts
  • apps/web/src/app/admin/users/[id]/heuristic-abuse/page.tsx
  • apps/web/src/app/admin/users/[id]/page.tsx
  • apps/web/src/app/api/auth/[...nextauth]/route.ts
  • apps/web/src/app/api/auth/native/token/route.test.ts
  • apps/web/src/app/api/auth/native/token/route.ts
  • apps/web/src/app/api/auth/verify-turnstile/route.ts
  • apps/web/src/app/api/cloud-agent-next/balance/route.auth.test.ts
  • apps/web/src/app/api/dev/consume-credits/route.ts
  • apps/web/src/app/api/internal/bot-session-callback/[botRequestId]/route.test.ts
  • apps/web/src/app/api/internal/bot-session-callback/[botRequestId]/route.ts
  • apps/web/src/app/api/internal/kiloclaw/instance-ready/route.ts
  • apps/web/src/app/api/internal/support/users/gdpr-removal/route.test.ts
  • apps/web/src/app/api/internal/support/users/gdpr-removal/route.ts
  • apps/web/src/app/api/sso/organizations/route.ts
  • apps/web/src/app/api/user-deployments/webhook/route.ts
  • apps/web/src/app/auth/openai/callback/route.test.ts
  • apps/web/src/app/auth/openai/callback/route.ts
  • apps/web/src/app/get-started/page.tsx
  • apps/web/src/app/page.tsx
  • apps/web/src/app/payments/topup/success/page.tsx
  • apps/web/src/app/users/accept-invite/[token]/page.tsx
  • apps/web/src/app/users/after-sign-in/route.test.ts
  • apps/web/src/app/users/after-sign-in/route.tsx
  • apps/web/src/components/dev/actions.ts
  • apps/web/src/components/organizations/OrganizationInfoCard.tsx
  • apps/web/src/components/organizations/OrganizationPaymentDetails.tsx
  • apps/web/src/components/organizations/members/InviteMemberDialog.tsx
  • apps/web/src/components/profile/ProfileExpiringCredits.tsx
  • apps/web/src/components/subscriptions/org-kilo-pass/mappers.ts
  • apps/web/src/components/usage-analytics/format.ts
  • apps/web/src/lib/ai-gateway/abuse-service.ts
  • apps/web/src/lib/ai-gateway/first-usage-events.test.ts
  • apps/web/src/lib/ai-gateway/handlers/llm-proxy.ts
  • apps/web/src/lib/ai-gateway/handlers/organization-model-validation.test.ts
  • apps/web/src/lib/ai-gateway/handlers/organization-model-validation.ts
  • apps/web/src/lib/ai-gateway/handlers/organization-models.test.ts
  • apps/web/src/lib/ai-gateway/handlers/organization-models.ts
  • apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts
  • apps/web/src/lib/ai-gateway/openai-chatgpt/routing.test.ts
  • apps/web/src/lib/ai-gateway/processUsage.shared.ts
  • apps/web/src/lib/ai-gateway/processUsage.test.ts
  • apps/web/src/lib/ai-gateway/processUsage.ts
  • apps/web/src/lib/ai-gateway/processUsage.types.ts
  • apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.test.ts
  • apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.ts
  • apps/web/src/lib/ai-gateway/providers/build-direct-provider.test.ts
  • apps/web/src/lib/ai-gateway/providers/direct-byok/opencode-go.test.ts
  • apps/web/src/lib/ai-gateway/providers/types.ts
  • apps/web/src/lib/ai-gateway/providers/upstream-attempt.ts
  • apps/web/src/lib/ai-gateway/typesafe/handler.test.ts
  • apps/web/src/lib/ai-gateway/typesafe/handler.ts
  • apps/web/src/lib/auth/data-export-download-codes.ts
  • apps/web/src/lib/auth/email-signin-eligibility.ts
  • apps/web/src/lib/auth/magic-link-tokens.ts
  • apps/web/src/lib/auth/verify-turnstile-jwt.test.ts
  • apps/web/src/lib/auth/verify-turnstile-jwt.ts
  • apps/web/src/lib/autoTopUp.ts
  • apps/web/src/lib/bot.test.ts
  • apps/web/src/lib/bot.ts
  • apps/web/src/lib/code-reviews/action-required.ts
  • apps/web/src/lib/code-reviews/review-memory/llm.ts
  • apps/web/src/lib/contributor-champions/service.ts
  • apps/web/src/lib/discord/auth.ts
  • apps/web/src/lib/email-address.ts
  • apps/web/src/lib/fraud-detection-headers.ts
  • apps/web/src/lib/kilo-pass/apple-store-notifications.test.ts
  • apps/web/src/lib/kilo-pass/cancel-and-refund.ts
  • apps/web/src/lib/kilo-pass/google-play-notifications.test.ts
  • apps/web/src/lib/kilo-pass/issuance.ts
  • apps/web/src/lib/kilo-pass/store-subscription-completion.ts
  • apps/web/src/lib/kilo-pass/subscription-accounting.ts
  • apps/web/src/lib/kilo-pass/yearly-monthly-base-cron.test.ts
  • apps/web/src/lib/microdollars.ts
  • apps/web/src/lib/notifications.ts
  • apps/web/src/lib/observability/rest-routes-auth.test.ts
  • apps/web/src/lib/organizations/organization-billing.ts
  • apps/web/src/lib/organizations/organization-member-removal.ts
  • apps/web/src/lib/organizations/organization-seats.ts
  • apps/web/src/lib/organizations/organization-settings-route-handler.ts
  • apps/web/src/lib/organizations/organization-subscription-event.test.ts
  • apps/web/src/lib/organizations/organization-usage.test.ts
  • apps/web/src/lib/organizations/organization-usage.ts
  • apps/web/src/lib/organizations/organizations.test.ts
  • apps/web/src/lib/organizations/organizations.ts
  • apps/web/src/lib/organizations/verified-domain-membership.test.ts
  • apps/web/src/lib/organizations/verified-domain.ts
  • apps/web/src/lib/promotionalCredits.ts
  • apps/web/src/lib/referral.ts
  • apps/web/src/lib/spend-alerts/delivery.test.ts
  • apps/web/src/lib/spend-alerts/settings.test.ts
  • apps/web/src/lib/stripe/disputes.ts
  • apps/web/src/lib/stripe/index.ts
  • apps/web/src/lib/stytch.ts
  • apps/web/src/lib/trpc-route-handler.ts
  • apps/web/src/lib/trpc/route-handler.ts
  • apps/web/src/lib/user/deletion-queue/deletion-enqueue.ts
  • apps/web/src/lib/user/deletion-queue/deletion-legacy-backfill.test.ts
  • apps/web/src/lib/user/find-user-by-id.ts
  • apps/web/src/lib/user/gdpr-removal.ts
  • apps/web/src/lib/user/index.test.ts
  • apps/web/src/lib/user/index.ts
  • apps/web/src/lib/user/next-auth-options.ts
  • apps/web/src/lib/user/profile-redirect-path.ts
  • apps/web/src/lib/user/server-signin-callback.test.ts
  • apps/web/src/lib/user/server.test.ts
  • apps/web/src/lib/user/server.ts
  • apps/web/src/lib/utils.ts
  • apps/web/src/routers/admin-router.ts
  • apps/web/src/routers/admin/user-deletion-queue-router.test.ts
  • apps/web/src/routers/bitbucket-router.test.ts
  • apps/web/src/routers/kilo-pass-router.ts
  • apps/web/src/routers/mcp-gateway-authorizations-router.test.ts
  • apps/web/src/routers/mcp-gateway-router.test.ts
  • apps/web/src/routers/organizations/organization-admin-router.ts
  • apps/web/src/routers/organizations/organization-members-router.ts
  • apps/web/src/routers/organizations/organization-sub-organizations-router.ts
  • apps/web/src/routers/test-utils.ts
  • apps/web/src/scripts/d2025-09-01_backfill-referral-code-usages.ts
  • apps/web/src/tests/helpers/microdollar-usage.helper.ts

Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

The post-sign-in landing path pulls in sales demos, verified domains, seat
purchases and trial classification, none of which request
authentication uses. It moves unchanged, with its personal-account
fallback, to lib/user/profile-redirect-path.
…er/server

getUserFromAuth reads sessions with getServerSession(authOptions), so every
request that authenticates loaded all sign-in providers and callbacks:
account creation and linking, passkeys, magic links, SSO, verified-domain
joins, Impact attribution, PostHog and the ChatGPT connection store.

lib/user/server now reads sessions with sessionAuthOptions, the options a
session read uses: secret, logger, pages, debug and the jwt and session
callbacks. getServerSession drops providers, and a session read calls the
jwt callback without a trigger, for which the full callback already returns
the token unchanged, so session reads behave as before.

The sign-in configuration moves unchanged to lib/user/next-auth-options,
where authOptions extends sessionAuthOptions with the cookies, providers
and the signIn and full jwt callbacks.
Jest also runs the @/lib/posthog mock factory while it generates the
automatic @/lib/user mock. Now that lib/user/server no longer imports
lib/user, that happens after the route created its PostHog client, so the
factory replaced the capture mock the test asserts on. The factory now
reuses one capture mock.
@chrarnoldus
chrarnoldus force-pushed the refactor/web-shared-prep branch from 40b9c7d to fb70b48 Compare September 29, 2026 15:11
@chrarnoldus chrarnoldus changed the title refactor(web): decouple gateway routes from unrelated modules refactor(web): split NextAuth sign-in out of lib/user/server Sep 29, 2026
@chrarnoldus chrarnoldus changed the title refactor(web): split NextAuth sign-in out of lib/user/server refactor(web): move NextAuth sign-in config and landing redirect out of lib/user/server Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants