# Problems with WithPageAuthRequired and nextjs dynamic routing

**URL:** <https://community.auth0.com/t/problems-with-withpageauthrequired-and-nextjs-dynamic-routing/131009>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [April 2, 2024, 3:39pm UTC](https://community.auth0.com/t/problems-with-withpageauthrequired-and-nextjs-dynamic-routing/131009 "2024-04-02T15:39:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![axxcident](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/axxcident/32/56065_2.png) [@axxcident](https://community.auth0.com/u/axxcident)\
**Post date:** [April 2, 2024, 3:39pm UTC](https://community.auth0.com/t/problems-with-withpageauthrequired-and-nextjs-dynamic-routing/131009/1 "2024-04-02T15:39:46Z")

</div>

Hello I’m trying to integrate a next.js project with auth0.

I’m running on next.js v14 and auth0 v. 3.5

The issue I’m having is with protecting my pages with WithPageAuthRequired and using [Next.js’s dynamic routing](https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes) at the same time.

I’ve been looking at examples and I’m trying to combine something like this:

```auto
export async function generateMetadata({
  params,
}: {
  params: { user_id: string };
}) {
  return {
    title: 'User Dashboard',
    description: `Dashboard for User: ${params.user_id}`,
  };
}
const UserDashboard = ({ params }: { params: { user_id: string } }) => {
 ... rest of page

```

With this:

```auto
... rest of user page above.
export default withPageAuthRequired(UserDashboard, {
  returnTo: ({ params }: AppRouterPageRouteOpts) => {
    if (params) {
      const user_id = params['user_id'];
      if (typeof user_id === 'string') {
        return `${process.env.NEXT_PUBLIC_BASE_URL}/user-dashboard/${user_id}`
      }
    }
  }
});

```

But I get some typescript error on “UserDashboard”:

```auto
Argument of type '({ params }: { params: { user_id: string; };}) => React.JSX.Element' is not assignable to parameter of type 'AppRouterPageRoute'.
  Types of parameters '__0' and 'obj' are incompatible.
    Type 'AppRouterPageRouteOpts' is not assignable to type '{ params: { user_id: string; }; }'.
      Types of property 'params' are incompatible.
        Type 'Record<string, string | string[]> | undefined' is not assignable to type '{ user_id: string; }'.
          Type 'undefined' is not assignable to type '{ user_id: string; }'.ts(2345)
const UserDashboard: ({ params }: {
    params: {
        user_id: string;
    };
}) => React.JSX.Element

```

I’m very new to Auth0 and so I’m not sure if I should try to use “withPageAuthRequired” to check whether a user is logged in or not. I find the [documentation](https://auth0.github.io/nextjs-auth0/types/helpers_with_page_auth_required.WithPageAuthRequiredAppRouter.html) very abstract.

Have anyone had this issue before and know what to do?

In Summary, tried this:

```auto
export default withPageAuthRequired(UserDashboard, {
  returnTo: ({ params }: AppRouterPageRouteOpts) => {
    if (params) {
      const user_id = params['user_id'];
      if (typeof user_id === 'string') {
        return `${process.env.NEXT_PUBLIC_BASE_URL}/user-dashboard/${user_id}`
      }
    }
  }
});

```

And expected some validation to happen that checks if user i logged in and returns them to the user page or log-in page.

---

<div class="post-metadata">

**Author:** ![axxcident](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/axxcident/32/56065_2.png) [@axxcident](https://community.auth0.com/u/axxcident)\
**Post date:** [April 4, 2024, 12:45pm UTC](https://community.auth0.com/t/problems-with-withpageauthrequired-and-nextjs-dynamic-routing/131009/2 "2024-04-04T12:45:28Z")

</div>

Found the solution:

```auto
const UserDashboardSchema = z.object({
  user_id: z.string(),
});

type UserParams = z.infer<typeof UserDashboardSchema>;

const UserDashboard: (
  obj: AppRouterPageRouteOpts,
) => Promise<React.ReactElement> = async ({ params }) => {
  if (!params || !('user_id' in params)) {
    redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/`);
  }
... rest of page component.

```

Then before exporting the page I needed to wrap the page/component in `withPageAuthRequired` like so:

```auto
export default withPageAuthRequired(UserDashboard, {
  returnTo: (obj: AppRouterPageRouteOpts) => {
    if (obj && obj.params && 'user_id' in obj.params) {
      const { user_id } = obj.params;
      return `${process.env.NEXT_PUBLIC_BASE_URL}/user-dashboard/${user_id}`;
    }
    return `${process.env.NEXT_PUBLIC_BASE_URL}/`;
  },
});

```

Using Zod and adjusting the dynamic page for what the expected return type of `withPageAuthRequired` is and of `AppRouterPageRouteOpts`. Then I worked.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 18, 2024, 12:45pm UTC](https://community.auth0.com/t/problems-with-withpageauthrequired-and-nextjs-dynamic-routing/131009/3 "2024-04-18T12:45:52Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
