# Deny users with certain roles at login

**URL:** <https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118>\
**Category:** Get Help\
**Tags:** login\
**Created:** [September 22, 2022, 10:07am UTC](https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118 "2022-09-22T10:07:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![d3v](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/d3v/32/33857_2.png) [@d3v](https://community.auth0.com/u/d3v)\
**Post date:** [September 22, 2022, 10:07am UTC](https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118/1 "2022-09-22T10:07:49Z")

</div>

Hi,  
I’d like to throw an exception like “wrong username or password” when some users with certain roles trying to authenticate to specific applications. Creating a new tenant does not fit our scenario because our applications are connected to one database. I wrote an action but I can’t show error message on login page using actions. Can you suggest any way to resolve?

Would appreciate any help.

---

<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 27, 2022, 11:52pm UTC](https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118/3 "2022-09-27T23:52:29Z")

</div>

Hello @d3v welcome to the community!

> [@d3v](#):
>
> I wrote an action but I can’t show error message on login page using actions. Can you suggest any way to resolve?

What does your action look like? You should be able to accomplish this using the `api.access.deny` method on the [post-login API object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/api-object). It’s up to your application to read the query params and handle what to do next.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![d3v](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/d3v/32/33857_2.png) [@d3v](https://community.auth0.com/u/d3v)\
**Post date:** [September 28, 2022, 7:50am UTC](https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118/5 "2022-09-28T07:50:23Z")

</div>

> [@ty.frith](#):
>
> What does your action look like?

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

```

I’ve added a metadata called “required\_roles” (that contains the allowed roles) to all my applications. And the action checks that the user has one of these required roles. But I think `api.access.deny()` not exactly what I want. Can I redirect the user to login page and show error messages by the query parameter using `api.access.deny()`

---

<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 28, 2022, 9:31pm UTC](https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118/6 "2022-09-28T21:31:08Z")

</div>

Awesome, thanks for sharing!

> [@d3v](#):
>
> Can I redirect the user to login page and show error messages by the query parameter using `api.access.deny()`

There’s no OOTB support for redirecting a user to Universal Login with the error message - As it stands your application will need to handle what to do next with the error params/user returned to the callback. What I have seen in this scenario is a user being presented with some sort of error page (using the error params) and an option to click to bring up universal login once more with ([loginWithRedirect({prompt: ‘login’})](https://auth0.github.io/auth0-spa-js/classes/auth0client.html#loginwithredirect)) for example.

---

<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:** [October 12, 2022, 9:31pm UTC](https://community.auth0.com/t/deny-users-with-certain-roles-at-login/91118/7 "2022-10-12T21:31:27Z")

</div>

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