Check for role

Hi,
How can I check if a user has a specific role? Some users have more than 1 role on my site.
Thanks!!

Hi Quinten,
The property ‘role’ is deprecated.
It’s best to use the getRoles( ) function to get all the user’s roles which returns an array of all the user’s roles.
You can search this array to see if a user has a specific role.
Good luck,
Noam.

You can do something like:

const roleToCheck = "Admin" //or any role you want to check
  user.getRoles()
  .then(roles => {
    if(roles.some(r => r.name === roleToCheck)){
    //do if true
    } else {
    //do if false
    }
  })

Thanks!!

Does this mean J. D.'s solution doesn’t work?

@webmasterq Noam wrote this post before. It’s the same solution, I‘be just added details.

@jonatandor35 Oh lol sorry, I did not see that because I was on mobile and all other comments were hidden on there