# How to check verification based on user's role?

**URL:** <https://community.auth0.com/t/how-to-check-verification-based-on-users-role/123606>\
**Category:** Get Help\
**Tags:** identifier-first, login-experience\
**Created:** [December 23, 2023, 4:40pm UTC](https://community.auth0.com/t/how-to-check-verification-based-on-users-role/123606 "2023-12-23T16:40:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zedeleyici](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/zedeleyici/32/49217_2.png) [@zedeleyici](https://community.auth0.com/u/zedeleyici)\
**Post date:** [December 23, 2023, 4:40pm UTC](https://community.auth0.com/t/how-to-check-verification-based-on-users-role/123606/1 "2023-12-23T16:40:49Z")

</div>

Hello everyone, so we are trying to make a structure that login is verified on user’s role. Roles are assigned manually in dashboard (no problem because this is highly secured website, this is the correct way.)

So, as testing, we assigned a role as ‘user’. Now my code is structured as below.

def get\_user\_roles(request, user\_id):  
# Set your Auth0 domain  
auth0\_domain = settings.SOCIAL\_AUTH\_AUTH0\_DOMAIN  
mgmt\_api\_access\_token = settings.AUTH0\_MGMT\_API\_ACCESS\_TOKEN

```
# Initialize the HTTPS connection
conn = http.client.HTTPSConnection(auth0_domain)
headers = {'authorization': f"Bearer {mgmt_api_access_token}"}

try:
    # Request to fetch user roles
    conn.request("GET", f"/api/v2/users/{user_id}/roles", headers=headers)
    res = conn.getresponse()
    data = res.read()

    # Decode and extract roles from the response
    roles_data = json.loads(data.decode("utf-8"))
    roles = [role['name'] for role in roles_data] # Assuming roles are in the 'name' field
    return roles

except Exception as e:
    # Handle exceptions (log or raise as needed)
    print(f"Error fetching roles from Auth0: {e}")
    return []

```

def login(request):  
return oauth.auth0.authorize\_redirect(  
request, request.build\_absolute\_uri(reverse(“callback”))  
)

def callback(request):  
token = oauth.auth0.authorize\_access\_token(request)  
id\_token = token.get(‘id\_token’)  
access\_token = token.get(‘access\_token’)  
jwk\_set = oauth.auth0.fetch\_jwk\_set()  
user\_info = jwt.decode(id\_token, jwk\_set)  
user\_id = user\_info.get(‘sub’)  
user\_roles\_or\_permissions = get\_user\_roles(user\_id, access\_token)  
required\_permission = ‘user’  
if required\_permission not in user\_roles\_or\_permissions:  
messages.error(request, ‘You must verify your email to log in.’)

```
    return redirect('http://localhost:3000/callback') ## Conditions are identified in index.html

request.session['session_id'] = {
    'id_token': token.get('id_token'),
    # other user information as needed
}
return redirect('http://localhost:3000/callback?token=' + id_token)

```

However, **if required\_permission not in user\_roles\_or\_permissions:** is constantly true, so nobody can access the website right now. What did I wrong? I checked all the docs but could not get help. Please can you help me to fix this code? What is wrong?

---

<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:** [January 2, 2024, 9:07pm UTC](https://community.auth0.com/t/how-to-check-verification-based-on-users-role/123606/3 "2024-01-02T21:07:36Z")

</div>

Hey there @zedeleyici !

In your testing, does `user_roles_or_permissions` contain the correct role?

Auth0 also provides a [Python SDK](https://auth0.com/docs/libraries#backend) that could help verify/validate tokens and users roles (you would [add these](https://community.auth0.com/t/add-roles-to-access-token/76471/2) directly to tokens in an [Action](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow)).

---

<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:** [January 16, 2024, 9:08pm UTC](https://community.auth0.com/t/how-to-check-verification-based-on-users-role/123606/4 "2024-01-16T21:08:14Z")

</div>

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