# Preventing a user belonging to a group to access an application

**URL:** <https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [April 14, 2023, 7:35am UTC](https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703 "2023-04-14T07:35:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liechtjc](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@liechtjc](https://community.auth0.com/u/liechtjc)\
**Post date:** [April 14, 2023, 7:35am UTC](https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703/1 "2023-04-14T07:35:28Z")

</div>

Hello,

I’m trying to prevent some users to access a specific application.

I’ve created a group `is_not_application_user` and attached the users that should not be accessing the application.

I have added a custom action:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if ((event.client.name === "Application") && !userRoles.includes("is_not_application_user"))
  api.access.deny(`Access to ${event.client.name} is not allowed.`);
 };

```

This action is inserted in login flow after the legacy rules.

If I test this with the application, I can never connect ( ?sso\_failed=1). I can connect to the other application.

It should be a very basic thing but I m not good enough in programming ☹

Thank you in advance for your help.  
JC

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 20, 2023, 1:30pm UTC](https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703/3 "2023-04-20T13:30:49Z")

</div>

Hi @liechtjc,

Welcome to the Auth0 Community!

It looks like you are putting a _not operator_ (`!`) in front of your condition when you should simply be checking for the condition itself. (i.e. `userRoles.includes` vs. `!userRoles.includes`).

Additionally, the variable you are using `userRoles`, does not exist. If you want to look at the user’s roles, you should be using the variable `event.authorization.roles`.

Finally, you are missing curly brackets `{}` for your conditional.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![liechtjc](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@liechtjc](https://community.auth0.com/u/liechtjc)\
**Post date:** [April 23, 2023, 7:28pm UTC](https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703/4 "2023-04-23T19:28:29Z")

</div>

Dear Dan,

Thank you very much for your help. This solved my problem. I’m sorry that I bother you with such trivial questions ☹

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [April 24, 2023, 1:22pm UTC](https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703/5 "2023-04-24T13:22:45Z")

</div>

No problem! I’m happy to help.

---

<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:** [May 8, 2023, 1:22pm UTC](https://community.auth0.com/t/preventing-a-user-belonging-to-a-group-to-access-an-application/104703/6 "2023-05-08T13:22:58Z")

</div>

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