Skip to content

fix: make withPageAuthRequired generic to support PageProps/LayoutProps types#2529

Merged
Piyush-85 merged 3 commits intoauth0:mainfrom
sleitor:fix/with-page-auth-required-generic-types
Mar 19, 2026
Merged

fix: make withPageAuthRequired generic to support PageProps/LayoutProps types#2529
Piyush-85 merged 3 commits intoauth0:mainfrom
sleitor:fix/with-page-auth-required-generic-types

Conversation

@sleitor
Copy link
Contributor

@sleitor sleitor commented Feb 19, 2026

Closes #2440

Problem

withPageAuthRequired does not accept Next.js PageProps or LayoutProps types because AppRouterPageRoute expects AppRouterPageRouteOpts with loosely-typed params?: Promise<Record<string, string | string[]>>. Next.js typed routes produce narrower types (e.g. Promise<{ id: string }>) that are incompatible.

// TS error: Types of parameters 'props' and 'obj' are incompatible
export default auth0.withPageAuthRequired(
  async function Page(props: PageProps<"/customers/[id]/details">) {
    const { id } = await props.params;
    return <div>{id}</div>;
  }
);

Fix

Make AppRouterPageRoute, WithPageAuthRequiredAppRouter, and appRouteHandlerFactory generic over the props type P extends AppRouterPageRouteOpts. TypeScript infers P from the handler function, so the return type preserves the exact props type.

// Now works — props.params correctly typed as Promise<{ id: string }>
export default auth0.withPageAuthRequired(
  async function Page(props: PageProps<"/customers/[id]/details">) {
    const { id } = await props.params;
    return <div>{id}</div>;
  }
);

Also works with LayoutProps:

export default auth0.withPageAuthRequired(
  async function Layout(props: LayoutProps<"/dashboard">) {
    return <div>{props.children}</div>;
  }
);

Changes

  • src/server/helpers/with-page-auth-required.ts:
    • AppRouterPageRoute<P> — generic with default AppRouterPageRouteOpts
    • WithPageAuthRequiredAppRouter — generic, infers P from handler
    • appRouteHandlerFactory — propagates generic through implementation

Testing

  • All 819 existing tests pass
  • TypeScript compiles without errors
  • Backward compatible: existing code without generics continues to work (default type parameter)

@sleitor sleitor requested a review from a team as a code owner February 19, 2026 23:28
@Piyush-85 Piyush-85 mentioned this pull request Mar 10, 2026
Piyush-85
Piyush-85 previously approved these changes Mar 11, 2026
@Piyush-85
Copy link
Contributor

Hi @sleitor, had a look at the changes and looks good. There were a few additional use cases which I wanted to cover and have raised a follow up pr for those. PR 2550.
Will do a round of final testing and release it shortly.

…ps types

The AppRouterPageRoute type now accepts a generic parameter P that
extends AppRouterPageRouteOpts, allowing Next.js PageProps and
LayoutProps types to be used with withPageAuthRequired.

Before:
  // TS error: Types of parameters 'props' and 'obj' are incompatible
  export default auth0.withPageAuthRequired(
    async function Page(props: PageProps<'/customers/[id]'>) { ... }
  );

After:
  // Works correctly, props.params is typed as Promise<{ id: string }>
  export default auth0.withPageAuthRequired(
    async function Page(props: PageProps<'/customers/[id]'>) { ... }
  );

Closes auth0#2440
@sleitor sleitor force-pushed the fix/with-page-auth-required-generic-types branch from a6d4a7c to 7b64c2e Compare March 14, 2026 11:45
@codecov-commenter
Copy link

codecov-commenter commented Mar 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.50%. Comparing base (7480201) to head (517c7e6).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2529   +/-   ##
=======================================
  Coverage   90.50%   90.50%           
=======================================
  Files          52       52           
  Lines        6632     6634    +2     
  Branches     1380     1380           
=======================================
+ Hits         6002     6004    +2     
  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.

@Piyush-85
Copy link
Contributor

Hi @sleitor, observed this pr a few failing checks. Could you please have a look into those !!

@sleitor
Copy link
Contributor Author

sleitor commented Mar 19, 2026

@Piyush-85 Hi! I checked the CI status — the checks show as "action_required" which is standard for external PRs requiring maintainer CI approval to run workflows, not actual test failures. The local lint check passes (tsc --noEmit + ESLint). If you can approve the CI run, I believe all checks should pass. Let me know if you see any other specific failures I should address!

@Piyush-85 Piyush-85 merged commit 09e3ffa into auth0:main Mar 19, 2026
8 of 9 checks passed
@sleitor sleitor deleted the fix/with-page-auth-required-generic-types branch March 20, 2026 19:04
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