Skip to content

Commit 09e3ffa

Browse files
authored
fix: make withPageAuthRequired generic to support PageProps/LayoutProps types (#2529)
2 parents 7480201 + 517c7e6 commit 09e3ffa

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

src/server/helpers/with-page-auth-required.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,22 @@ export type AppRouterPageRouteOpts = {
4444
/**
4545
* An app route that has been augmented with {@link WithPageAuthRequired}.
4646
* Returns unknown to avoid React dependency while maintaining type safety.
47+
*
48+
* The generic parameter `P` allows passing Next.js `PageProps` or `LayoutProps`
49+
* types for strongly-typed route parameters:
50+
*
51+
* ```ts
52+
* export default auth0.withPageAuthRequired(
53+
* async function Page(props: PageProps<"/customers/[id]/details">) {
54+
* const { id } = await props.params;
55+
* return <div>{id}</div>;
56+
* }
57+
* );
58+
* ```
4759
*/
48-
export type AppRouterPageRoute = (
49-
obj: AppRouterPageRouteOpts
50-
) => Promise<unknown>;
60+
export type AppRouterPageRoute<
61+
P extends AppRouterPageRouteOpts = AppRouterPageRouteOpts
62+
> = (obj: P) => Promise<unknown>;
5163

5264
/**
5365
* If you have a custom returnTo url you should specify it in `returnTo`.
@@ -166,10 +178,12 @@ export type WithPageAuthRequiredAppRouterOptions = {
166178
* export default ProtectedPage;
167179
* ```
168180
*/
169-
export type WithPageAuthRequiredAppRouter = (
170-
fn: AppRouterPageRoute,
181+
export type WithPageAuthRequiredAppRouter = <
182+
P extends AppRouterPageRouteOpts = AppRouterPageRouteOpts
183+
>(
184+
fn: AppRouterPageRoute<P>,
171185
opts?: WithPageAuthRequiredAppRouterOptions
172-
) => AppRouterPageRoute;
186+
) => AppRouterPageRoute<P>;
173187

174188
/**
175189
* Protects Page router pages {@link WithPageAuthRequiredPageRouter} or
@@ -185,8 +199,11 @@ export const appRouteHandlerFactory =
185199
loginUrl: string;
186200
}
187201
): WithPageAuthRequiredAppRouter =>
188-
(handler, opts = {}) =>
189-
async (params) => {
202+
<P extends AppRouterPageRouteOpts = AppRouterPageRouteOpts>(
203+
handler: AppRouterPageRoute<P>,
204+
opts: WithPageAuthRequiredAppRouterOptions = {}
205+
) =>
206+
async (params: P) => {
190207
const session = await client.getSession();
191208

192209
if (!session?.user) {

0 commit comments

Comments
 (0)