Get the information of user roles

I found that I can use code
to get the userRole, Members.
Now, Wix allows us to create different roles and assign the access right of pages with different user roles. Let’s say for example, I create a new role called student. How can I use code to check if the user is a student? If I use the useRole command, it returns , Members.
Please tell me the code.

I found a way to complete the task.

  1. Export my contact list
  2. Create a new collection and then import the data. Add a new field to categories the users.
  3. Use wix built in userRole feature to limit the pages that the users can access
  4. Build a backend web module to check the currentUser email and then return the category.
  5. So the page can be directed to a desired page based on the user email.
  6. I guess that Wix will officially release the getUserRole().
  7. Can anyone comment my approach?
  8. Is there any way automatically import new registered users contact to my collection?
    Hope Wix team can help

Hi Chiu Chi Hung,

You can get a logged-in user’s custom role using the wixUsers module function, getRoles
for example:

import wixUsers from 'wix-users';

$w.onReady(function () {
    wixUsers.currentUser.getRoles()
        .then((roles) => {
            console.log(roles[0]) //output will be: {name: "Student", description: "Student site member"}
        })
});

Hope that helps!

  • Lior

Thanks Lior,
I have tried this. It works.
But I have another 2 questions.

  1. First, I add some members with more than 1 roles. When I assign [1]. It does not work. What does roles[1] means?
  2. Currently, I am using another collection to store the contact? Any suggestion on this arrangement? Million thanks

Hi Chui Chi
When you say you assign [1] are you saying that you try to read roles[1] or write to roles[1]?

A simple test for you will be to extend Lior’s proposed script like so:

import wixUsers from 'wix-users';
  $w.onReady(function () {
       wixUsers.currentUser.getRoles()
        .then((roles) => {
             console.log('Received '+roles.length+' roles');
             for (let i = 0 ; i < roles.length ; i++) {
                  console.log(roles[i]); //output will be: {name: "Student", description: "Student site member"}
             }
        })
  });

wix-users api is one way (read only). You must manage roles in the Wix CRM dashboard before you can access the roles in code.

Hope this helps
Steve

Thanks Steve,
This help me to list out the role(s).
Actually, I am a new beginner in coding.
As states previously, I duplicate the CRM in another collection so that I can read their roles, status and then direct them to different pages. But it involves additional work to update the collection for every new users. IIs there any suggestion ? So that I can manage the CRM by coding? I read other posts in the community. Others are using the similar way. And they are facing the same issue. Thanks for your time.
Joe

This code is working great for getting member roles

import wixUsers from 'wix-users';  $w.onReady(function () {
       wixUsers.currentUser.getRoles() .then((roles) => {     
              console.log('Received '+roles.length+' roles'); 
              for (let i = 0 ; i < roles.length ; i++) {                  
              console.log(roles[i]); 
               } 
            }) 
        });

My issue is if someone has multiple roles my output shows them correctly, my console log will show

Role 1
Role 2
Role 3

My issue is, I want to see if they are Role 1 do this. What I have tried is:

if(role[i]=== role1){
// function here
}

But this doesn’t seem to be working correctly. am I missing something?

This post is from 2018 and is being closed.
If you need more help with this then please add a new forum post.

Also, search the forum for previous posts as there are many that will help you already.
https://www.wix.com/corvid/forum/community-discussion/getroles-function-is-not-working/p-1/dl-5e148145dd7d620017498340
https://www.wix.com/corvid/forum/community-discussion/user-roles
https://www.wix.com/corvid/forum/community-discussion/urgent-send-a-user-to-location-based-on-member-role