User roles

Hi, Just trying to show hide buttons depending on the users level ie admin, member or visitor.

Can’t get this to work tho, is something wrong with the code?

import wixUsers from 'wix-users';

$w.onReady(function(){
 const currentUser = wixUsers.currentUser;
 if (currentUser.loggedIn) {
 if (currentUser.role === 'Admin') {
 //show or hide elements here:
            $w('#button1').show();
        }
 if (currentUser.role === 'Member') {
            $w('#button1').hide();
        }
 if (currentUser.role === 'Visitor') {
            $w('#button1').hide();
        }
    }
 
});

Hi,
You should set the elements to be ‘Hidden on load’ (using properties panel) instead of your current code.
Here’s a skeleton for a code:


import wixUsers from 'wix-users';
$w.onReady(function(){
    const currentUser = wixUsers.currentUser;
    if (currentUser.loggedIn && currentUser.role === 'Admin'){
        //Show components:
        $w('#button1').show();
    };
});

Good luck :slight_smile:

1 Like

Thanks Or. That does work, although it some reason only has me as a member, not admin.

Also,I have to refresh the page once I have logged in for the buttons to show, is there a way to fix this?

My site is aabiology on my account, on the "Members’ page

import wixUsers from 'wix-users';
$w.onReady(function(){
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn && currentUser.role === 'Admin'){
//Show components:
       $w('#button1').show();
   }
if (currentUser.loggedIn && currentUser.role === 'Member'){
//Show components:
       $w('#button2').show();
   }
if (currentUser.loggedIn && currentUser.role === 'Visitor'){
//Show components:
       $w('#button3').show();
   }
});

All good, found the answer here: https://www.wix.com/code/home/forum/community-discussion/how-to-auto-refresh-page-onevent

1 Like

Sorry Or, is there a way to use the mmeber role name I have assigned instead of ‘Member’, Admin & Visitor? ie I have made a role called COuncil, but I can’t seem to make it work if I change from ‘Admin’ to ‘Council’?
Also, any reason you can see why I am a member and not admin? It says I am admin in the dashboard

1 Like

Hello!
Is there a way of changing the type of role that a user have after clicking a button for instance??

Have you been able to solve this issue? is there any update on the user.role functionality? I am having a hard time accessing my roles in the code. Are the role options limited to member, admin, and visitor? I have created my own role named “Clinet”. How can I access it in the code?Thank you very much!