[URGENT] Send a User to Location based On Member Role

Hi, I’m trying to solve this for a few days now. What I need is to read the user member role (Ambassador), created in the roles section of the Dashboard and use a button to send the user to a specific location. When the role is different, the button remains the same but the location changes.

So far I’m not getting a single result! HELP!!!

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

let url = wixLocation.url;
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

// this is where the button gets displayed if the user is a member

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#dashboardButton”).label = “My Dashboard”;
$w(“#dashboardButton”).show();
}
else {
$w(“#dashboardButton”).hide();
}
} );

// This should be getting the actual member role (Ambassador)

user.getRoles()
.then( (roles) => {
let firstRole = roles[0];
let roleName = firstRole.name; // “Role Name”
let roleDescription = firstRole.description; // “Role Description”
} );

// This is where I get lost!!

export function dashboardButton_click(event) {
const currentUser = wixUsers.currentUser;
if (currentUser.roleName === ‘Ambassador’){
$w(‘#button28’).show();
} else {
console.log();
}

}

https://www.wix.com/corvid/forum/community-discussion/showing-certain-data-in-a-collection-to-specific-members-only

Hi, I tried to use that but it returns a parsing error :frowning:

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

let url = wixLocation.url;
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#dashboardButton”).label = “My Dashboard”;
$w(“#dashboardButton”).show();
}
else {
$w(“#dashboardButton”).hide();
}

var roleName;
let currentUser = wixUsers.currentUser;

currentUser.getRoles()
.then( (roles) => {
var firstRole = roles[0];
roleName = firstRole.name;
if (roleName === “Ambassador”){
$w(‘#dashboardButton’).show();
}
} );

You need to make sure that you go through your code and check that your code has matching pairs of open and closed curly brackets ( ‘{’ and ‘}’) as well as matching pairs of parentheses (‘(’ and ‘)’).

See here for more info from an old post that explains it to you.
https://www.wix.com/corvid/forum/community-discussion/parsing-error-unexpected-token

Thanks… I have been trying to match them but for some reason is not working. I will give it another try!

@andres you didn’t close the $w.onReady() function.

@jonatandor35 Thanks JD I just did but the code still doesn’t work. I’m getting a bit desperate here ahahah.

Supposedly the role [0] should be the role assigned to the user since it only has one assigned member role but I can’t really find my way around this thing :confused:

In theory until here everything is correct but then I just can’t find the way.

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

let user = wixUsers.currentUser;
let url = wixLocation.url;

let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”

let isLoggedIn = user.loggedIn; // true

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#dashboardButton”).hide();
}
else {
$w(“#dashboardButton”).label = “My Dashboard”;
$w(“#dashboardButton”).show();
}
} );

user.getRoles()
.then( (roles) => {
let firstRole = roles[0];
let roleName = firstRole.name; // “Role Name”
let roleDescription = firstRole.description; // “Role Description”
} );

@andres first of all, since a member may have more than 1 role, you shouldn’t select toles[0].
Inside the the .then().{} block put:

.then( (roles) => { 
 if (roles.some(r =>  r.name === "Admin")) {//instead of "Admin" use the relevant role name
wixLocation.to(url);
}
} ); 

@jonatandor35 Hey JD, first, thanks for trying to help… I’m really not understanding what am I doing wrong here. I applied your code but I still get a parsing error

and here is the role permission (I know that in the picture above I leaved ‘Admin’ I was just trying something different.

@andres you have extra } ); before user.getRoles() . Remove them.

Also add a parentheses here:

if (roles.some(r =>  r.name === "Admin"))

@jonatandor35 now it changed here ahahha sorry to give you this headache

@andres you need another }) in the end of your code.

@jonatandor35 at least now I managed to reload the page every 2 seconds ahahaha

@andres OK. So replace the link, and everything is fine. Glad it worked.

@jonatandor35 Thank you for this. :+1:

@J. D. HEy man, sorry to bother you one more time :slight_smile: I tried the code yesterday and it all seemed to work just fine for one role but adding a second one completely threw the code off. I have a different approach that should work:

I have one button for each role which is hidden on load and that should appear when the respective role is signed in. So far I only get the same button to appear, independent of who is logged in :frowning: Would you take a look?

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

let user = wixUsers.currentUser;
let url = wixLocation.url;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
user.getRoles()
.then( (roles) => {
if (roles.some(r => r.name === “Ambassador”)) {
$w(‘#dashboardButton’).show();
} else if (roles.some(r => r.name === “Influencer”)){
$w(‘#dashboardButton’).hide();
$w(‘#influencerButton’).show();
}
})
}
});

Even tried to clean it but even when the user is an “influencer” it shows me the button for ambassador.

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

let user = wixUsers.currentUser;
let url = wixLocation.url;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
user.getRoles()
.then( (roles) => {
if (roles.some(r => r.name === “Ambassador”)) {
$w(“#dashboardButton”).show();
}
} );
}
else {
$w(“#dashboardButton”).hide();
}
});