Skip to content

Fix/gh 2440#2550

Merged
Piyush-85 merged 4 commits intomainfrom
fix/gh-2440
Mar 23, 2026
Merged

Fix/gh 2440#2550
Piyush-85 merged 4 commits intomainfrom
fix/gh-2440

Conversation

@Piyush-85
Copy link
Contributor

Closes #2440

📋 Changes

Problem

While the need for generic support was identified in PR #2529 the previous implementation did not fully allow for type inference. Specifically, the returnTo callback was still restricted to the base AppRouterPageRouteOpts type, and the main wrapper did not correctly "tunnel" the specific Page/Layout props through to the returned component.

Fix

I have updated the App Router types to be fully generic. By making AppRouterPageRoute, WithPageAuthRequiredAppRouterOptions, and WithPageAuthRequiredAppRouter generic over <P>, TypeScript can now correctly infer specific route parameters (like id or slug) from the input component and apply them throughout the helper.

Changes

  • AppRouterPageRoute<P>: Updated the definition to be generic, allowing it to accept narrow types like Next.js PageProps<T>.
  • WithPageAuthRequiredAppRouterOptions<P>: Added the generic parameter so that the returnTo callback is now strongly typed based on the specific page being protected.
  • WithPageAuthRequiredAppRouter: Updated the signature to capture the component's props type (P) and propagate it to both the handler and the options.
  • Documentation: Added TSDoc comments to explain how the generic P works for developers using Typed Routes.

📎 References

  • Refinement of PR #2529: Provides the specific generic implementation required to solve the type-narrowing issues in the App Router.
  • Feature: Full support for Next.js Typed Routes and Next.js 15 Promise props.

🎯 Testing

  • Inference Check: Verified that returnTo: async ({ params }) => ... now provides full autocomplete for specific dynamic route segments.
  • Compatibility: Confirmed that PageProps<"/route"> no longer triggers "Type mismatch" errors when passed to the wrapper.
  • Backward Compatibility: Ensured that existing code continues to work by using = AppRouterPageRouteOpts as a default type.
  • Test Suite: All 888 existing tests passed.

Manual Test Case

You can verify the fix by checking the type of params inside the returnTo function:

// With this PR, 'params' is correctly inferred as { id: string }
export default auth0.withPageAuthRequired(
  async function Page(props: PageProps<"/user/[id]">) { ... },
  {
    returnTo: async ({ params }) => `/user/${(await params).id}` 
  }
);

@Piyush-85 Piyush-85 requested a review from a team as a code owner March 10, 2026 11:04
@codecov-commenter
Copy link

codecov-commenter commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.50%. Comparing base (09e3ffa) to head (31e7ff6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2550   +/-   ##
=======================================
  Coverage   90.50%   90.50%           
=======================================
  Files          52       52           
  Lines        6634     6634           
  Branches     1380     1380           
=======================================
  Hits         6004     6004           
  Misses        619      619           
  Partials       11       11           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

tusharpandey13
tusharpandey13 previously approved these changes Mar 11, 2026
@tusharpandey13
Copy link
Contributor

lgtm

tusharpandey13
tusharpandey13 previously approved these changes Mar 19, 2026
@Piyush-85 Piyush-85 merged commit cb0c7db into main Mar 23, 2026
9 checks passed
@Piyush-85 Piyush-85 deleted the fix/gh-2440 branch March 23, 2026 10:32
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.

Support for PageProps and LayoutProps in withPageAuthRequired

3 participants