# Api.access.deny causes redirect loop

**URL:** <https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [April 7, 2023, 11:46am UTC](https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298 "2023-04-07T11:46:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgls](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mgls/32/46156_2.png) [@mgls](https://community.auth0.com/u/mgls)\
**Post date:** [April 7, 2023, 11:46am UTC](https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298/1 "2023-04-07T11:46:26Z")

</div>

**Use Case**  
We have multiple applications, let’s say public and private and want users with the `private` role to be able to access the `private` application on login. All other users (without the `private` role) will be denied at login.

**Current Solution**  
We created a login action to check if the user has the needed `private` role. The `private` application has a metadata key `access_role` with the value `private`. The action:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if(!event.authorization?.roles.includes(event.client.metadata.access_role))
  {
    api.access.deny('Access denied.');
  }
};

```

**Problem**  
With the action enabled, logging into the `private` application causes an endless redirect loop, just like:

- /callback?error=access\_denied&error\_description=Access denied.&state=123
- /
- /callback?error=access\_denied&error\_description=Access denied.&state=123
- /
- …

How to prevent this loop of death?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 7, 2023, 5:21pm UTC](https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298/3 "2023-04-07T17:21:20Z")

</div>

Hi @mgls,

Welcome to the Auth0 Community!

I understand that you have encountered a redirect loop with your Post-Login Action.

The behavior you have observed happens because the user has not logged out and retains the session from the past, which is when they were denied access.

Hence why, when the user tries to log in again, they’re immediately sent to the error page. The same is true when a user successfully logs in. If they do not log out, they will continue to have access to the page.

In this situation, I recommend calling the [`/v2/logout` endpoint](https://auth0.com/docs/api/authentication#logout). Please see our [Logout](https://auth0.com/docs/authenticate/login/logout) documentation for more information.

Lastly, let me also add that I have checked your tenant settings and found that your `Check Env Access Permission` Post-Login Action calls the `api.redirect.sendUserTo()` method without resuming the authentication flow.

To resolve this, you must call the `/continue` endpoint to resume the authentication. Please check out our [Redirect with Actions](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/redirect-with-actions#resume-the-authentication-flow) documentation which covers these steps in more detail.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mgls](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mgls/32/46156_2.png) [@mgls](https://community.auth0.com/u/mgls)\
**Post date:** [April 7, 2023, 5:58pm UTC](https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298/4 "2023-04-07T17:58:04Z")

</div>

Thanks @rueben.tiow

Our solution: we don’t use `api.access.deny()` anymore but use `api.redirect.sendUserTo('https://{auth0_tenant}.eu.auth0.com/v2/logout?returnTo=https%3A%2F%2Fexample.com')`.

With this, the not-allowed user will be logged out immediately after the login and gets redirected to the specified `returnTo` URL.

---

<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 21, 2023, 5:59pm UTC](https://community.auth0.com/t/api-access-deny-causes-redirect-loop/104298/5 "2023-04-21T17:59:01Z")

</div>

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