I have this code for if you are a member you can see this button, but i need to refresh the page for see the button when you log in each time
import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
$w.onReady( function (){
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn && currentUser.role === ‘Member’ ){
//Show components:
$w( “#button13 ” ).show();
}
});
J.D
October 15, 2020, 8:54pm
2
You have to run it on login:
//import etc...
let user = wixUsers.currentUser;
function showButton(){
if(user.role === "Member"){
$w("#button13").show();
}
}
$w.onReady() => {
showButton();
wixUsers.onLogin(u => {
user = u;
showButton();
})
Ohh ok I will try it . By the way, are there one option for roles creates by me? Not admin, member or visitor.
J.D
October 15, 2020, 8:58pm
4
I’m not sure I got what you were asking
I mean for example show button only for a Role( but not admin, visitor or member role) a custom role that I created
J.D
October 15, 2020, 9:06pm
6
import wixUsers from 'wix-users';
// ...
let user = wixUsers.currentUser;
const irrelevantRoles = ["Admin", "Member", "Visitor"];
user.getRoles()
.then(roles => {
const roleNames = roles.map(e => e.name);
const relevantUserRoles = roleNames.filter(e => !irrelevantRoles.includes(e));
if(relevantUserRoles.length > 0){$w("#button13").show();}
})
Ohh ok thanks. So( const roleNames = roles.map(e => e.name); ) here in name is where I have to put the name of role, right?
J.D
October 15, 2020, 9:11pm
8
@aapgcup No. Just use the code above as is. No need to change anything.
J.D
October 15, 2020, 9:16pm
10
I will add comments:
import wixUsers from 'wix-users';
// ...
let user = wixUsers.currentUser;
//declare which role is not interesting:
const irrelevantRoles = ["Admin", "Member", "Visitor"];
//get the roles
user.getRoles()
.then(roles => {
//extract the name property of the role:
const roleNames = roles.map(e => e.name);
//filter out the irrelevant role names:
const relevantUserRoles = roleNames.filter(e => !irrelevantRoles.includes(e));
//if there's any role left (meaning -- there's a relevant role) show the button:
if(relevantUserRoles.length > 0){$w("#button13").show();}
})
aapgcup
October 16, 2020, 7:59am
11
This told me it has an error in $w.onReady() => { to down
/import etc...
let user = wixUsers.currentUser;
function showButton(){
if(user.role === "Member"){
$w("#button13").show();
}
}
$w.onReady() => {
showButton();
wixUsers.onLogin(u => {
user = u;
showButton();
})