# Custom Action: user stays logged into Auth0 after api.access.deny

**URL:** <https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [September 5, 2024, 8:37pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425 "2024-09-05T20:37:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcantrell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mcantrell/32/60002_2.png) [@mcantrell](https://community.auth0.com/u/mcantrell)\
**Post date:** [September 5, 2024, 8:37pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/1 "2024-09-05T20:37:51Z")

</div>

We’re workin on implementing a domain whitelist similar to this template:

> <https://github.com/auth0/opensource-marketplace/blob/main/templates/simple-domain-allowlist-POST_LOGIN/code.js>

The problem is that the user stays logged into auth0 and subsequent login attempts do not challenge the user for new credentials. They may want to try again with a different domain but they’re now stuck in a access denied loop that will never complete.

Is there a way to prevent the login so this condition doesn’t occur?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 5, 2024, 11:12pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/3 "2024-09-05T23:12:53Z")

</div>

Hey there @mcantrell !

> [@mcantrell](#):
>
> Is there a way to prevent the login so this condition doesn’t occur?

As `api.access.deny` does not end any existing session, you’ll also want to log users out in order to avoid the loop you’re experiencing. Please see the following post:

> [@How to handle access denied response](https://community.auth0.com/t/how-to-handle-access-denied-response/136668/3):
>
> Hi @gabor.ottlik.hun, When you use the api.access.deny() method, it will deny users from logging in, but their session will remain intact. So, in subsequent login attempts, the user will continuously be sent to the error page (http://localhost:3000/api/auth/callback?error=access\_denied&error\_description=access%20denied.&state=eyJyZXR1cm5UbyI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCJ9) until the user logs out. That said, you will need to log the user out whenever they have been denied access to kill the …

---

<div class="post-metadata">

**Author:** ![mcantrell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mcantrell/32/60002_2.png) [@mcantrell](https://community.auth0.com/u/mcantrell)\
**Post date:** [September 6, 2024, 12:12am UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/4 "2024-09-06T00:12:41Z")

</div>

Hmmm, since the actions apply to all applications, I’m not sure how this will work for us. We have multiple apps that need this rule and they all have different URLs. I’d have to know which app they were authenticating to in order to know where to redirect them after logout.

Is there a method to determine which application the login event is for? I browsed around the event and didn’t see anything but I definitely could be missing something.

---

<div class="post-metadata">

**Author:** ![mcantrell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mcantrell/32/60002_2.png) [@mcantrell](https://community.auth0.com/u/mcantrell)\
**Post date:** [September 6, 2024, 12:38am UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/5 "2024-09-06T00:38:00Z")

</div>

I may have a workaround from the client side of things. We’re using react for the client and there there is an option exposed to force re-authentication using the popup:

```auto
    async function login() {
        // await auth.loginWithRedirect({
        // appState: {returnTo: window.location.pathname}
        // });
        let options: PopupLoginOptions = {
            authorizationParams: {
                prompt: 'login'    
            }
        }
        await auth.loginWithPopup(options);
        console.info('loginWithPopup result:', auth.isAuthenticated, auth.error);
    }

```

The popup is not our preferred login experience but I suppose we can make it work. I didn’t see any exposed options for doing the same thing using `loginWithRedirect`.

---

<div class="post-metadata">

**Author:** ![mcantrell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mcantrell/32/60002_2.png) [@mcantrell](https://community.auth0.com/u/mcantrell)\
**Post date:** [September 6, 2024, 1:01pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/6 "2024-09-06T13:01:42Z")

</div>

well it looks like `loginWithPopup` is also a dead end because we need to initiate auth from events like route changes and the browser popup manager will kick in.

The only two options I see are:

1. Determine which app the login event is for so I can map a redirect properly after the logout redirect
2. Find a way to pass the `prompt=login` param to the `loginWithRedirect` function. It doesn’t appear to be expose in the `RedirectLoginOptions` options in the javascript sdk.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 6, 2024, 2:05pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/7 "2024-09-06T14:05:43Z")

</div>

> [@mcantrell](#):
>
> Is there a method to determine which application the login event is for?

There is! You can access the client\_id of the application with `event.client.client_id` in action code.

---

<div class="post-metadata">

**Author:** ![mcantrell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mcantrell/32/60002_2.png) [@mcantrell](https://community.auth0.com/u/mcantrell)\
**Post date:** [September 6, 2024, 2:17pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/8 "2024-09-06T14:17:19Z")

</div>

I actually ended up using the original redirect URL found in the event:

```auto
api.redirect.sendUserTo(`https://${event.tenant.id}.us.auth0.com/v2/logout`, {
    query: { returnTo: event.transaction.redirect_uri }
});

```

I’m onto a different issue now because it’s denying the logout. The URL generated looks good:

```auto
https://redacted-tenant.us.auth0.com/v2/logout?returnTo=http%3A%2F%2Flocalhost%3A5173&state= **redacted**

```

The error log states:

```auto
The "returnTo" querystring parameter "http://localhost:5173" is not defined as a valid URL in "Allowed Logout URLs". To add a new URL, please do it here: https://manage.auth0.com/#/account/advanced

```

However, I’ve verified that the allowed logout URL is set to `http://localhost:5173` in the Auth0 dashboard so I’m not sure why it’s giving this error.

---

<div class="post-metadata">

**Author:** ![mcantrell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mcantrell/32/60002_2.png) [@mcantrell](https://community.auth0.com/u/mcantrell)\
**Post date:** [September 6, 2024, 2:25pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/9 "2024-09-06T14:25:48Z")

</div>

Ahh, I needed to pass the client\_id parameter so it knows which app to use for a logout whitelist. The following solution is working now:

```auto
api.redirect.sendUserTo(`https://${event.tenant.id}.us.auth0.com/v2/logout`, {
    query: {
        client_id: event.client.client_id,
        returnTo: `${event.transaction.redirect_uri}?auth_error_code=unapproved_social_domain` 
    }
});

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 6, 2024, 4:45pm UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/10 "2024-09-06T16:45:30Z")

</div>

That’s great @mcantrell glad that’s working for you and thanks for sharing with the community 🙂

---

<div class="post-metadata">

**Author:** ![amin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/amin/32/10144_2.png) [@amin](https://community.auth0.com/u/amin)\
**Post date:** [September 11, 2024, 5:56am UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/11 "2024-09-11T05:56:24Z")

</div>

use `api.session.revoke()`

---

<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:** [September 25, 2024, 5:56am UTC](https://community.auth0.com/t/custom-action-user-stays-logged-into-auth0-after-api-access-deny/145425/12 "2024-09-25T05:56:52Z")

</div>

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