# Next.js: Role access / claim check using withPageAuthRequired

**URL:** <https://community.auth0.com/t/next-js-role-access-claim-check-using-withpageauthrequired/89710>\
**Category:** Get Help\
**Tags:** nextjs\
**Created:** [August 30, 2022, 7:55pm UTC](https://community.auth0.com/t/next-js-role-access-claim-check-using-withpageauthrequired/89710 "2022-08-30T19:55:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinriddell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/colinriddell/32/33715_2.png) [@colinriddell](https://community.auth0.com/u/colinriddell)\
**Post date:** [August 30, 2022, 7:55pm UTC](https://community.auth0.com/t/next-js-role-access-claim-check-using-withpageauthrequired/89710/1 "2022-08-30T19:55:35Z")

</div>

I’m using [GitHub - auth0/nextjs-auth0: Next.js SDK for signing in with Auth0](https://github.com/auth0/nextjs-auth0) and I’d like to use `withPageAuthRequired` (or similar ) to be able to conditionally disallow someone access to a page based on the result of a function. Within that function, I’m imagining that I’ll read the session and grab their role from it.

Similar to the examples here: [helpers/with-page-auth-required | @auth0/nextjs-auth0](https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#getserversidepropsresultwithsession)

But one step further… I want to be able to return a 401 or some error component for pages their not allowed to access.

I tried this: [withAuthenticationRequired's claimsCheck causing infinite loop in Next.js SPA · Issue #320 · auth0/auth0-react · GitHub](https://github.com/auth0/auth0-react/issues/320#issuecomment-1013052056)  
But it does not work in next.js since that solution is for the react version of the library. Not only that but `useUser` or `useAuth0` won’t work since these are hooks that can only be used in a react component.

Looks like the react library allows you to implement a `claimCheck` via `WithAuthenticationRequiredOptions`. (src: [WithAuthenticationRequiredOptions | @auth0/auth0-react](https://auth0.github.io/auth0-react/interfaces/WithAuthenticationRequiredOptions.html#claimCheck)) Can it be done the same way for next or do I need to awkwardly mix the libraries?

I’m sure it must be possible with the options around `withPageAuthRequired` - but I can’t seem to figure it out.  
Any and all tips greatly appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![colinriddell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/colinriddell/32/33715_2.png) [@colinriddell](https://community.auth0.com/u/colinriddell)\
**Post date:** [August 31, 2022, 2:47pm UTC](https://community.auth0.com/t/next-js-role-access-claim-check-using-withpageauthrequired/89710/3 "2022-08-31T14:47:25Z")

</div>

Think I’ve managed to solve this one myself.

I basically figured I should be able to make a HOC that wraps `withPageAuthRequired` and returns a particular page based on checking roles (or anything you wish) from the user object. I called this HOC `withRoleAuthorization`

This works pretty well for my needs, I hope it works well for others, too:

```auto
const withRoleAuthorization = (
  Success,
  allowedRoles,
  Err,
  onError,
  onRedirecting,
  returnTo
) => {

  function hoc({ user }) {
    function arrContains(arr1, arr2) {
      return arr1.some(item => arr2.includes(item))
    }

    // check if the array of roles for a user contains any of the roles for the allowed array 
    // for the page.
    const usersRoles = user['http://mysite.com/roles']
    if (!arrContains(usersRoles, allowedRoles)){
      return (<Err />)
    }
    return (<Success user={user} />);
  }
  // https://auth0.github.io/nextjs-auth0/interfaces/frontend_with_page_auth_required.withpageauthrequiredoptions.html
  return withPageAuthRequired(hoc, {onError, onRedirecting, returnTo})
}

```

Then this can be used in a page like so:

```auto
function Profile({ user }) {
// we can use the user prop if we want it now, too :)
  return (
    <>
     <p>stuff here</p>
    </>
  );
}

// use of withRoleAuthorization - wrap your exported page component with it
export default withRoleAuthorization(Profile, ["admin", "somerole"], () => (<h1>nope!</h1>))

```

You’re welcome ! 😛

Pass `withRoleAuthorization`:

- the page component your doing role based access on,
- a list of roles that are allowed to access that page,
- and an error component that will be shown if the user doesn’t have that role, and is not allowed to access that page.

Can optionally pass in `onError`, `onRedirecting`, `returnTo` - so they can be fed into `withPageAuthRequired`.

Let me know if people think this can be improved!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 5:28pm UTC](https://community.auth0.com/t/next-js-role-access-claim-check-using-withpageauthrequired/89710/4 "2022-12-21T17:28:22Z")

</div>


