Question:
I need help getting the role information form a logged in member.
Product:
Wix Editor
What are you trying to achieve:
I need to detect different roles (“Student”, “Senior Staff”, etc.) so I can use the information in a if statement to Controll what sections of a dynamic page will be displayed.
What have you already tried:
I have searched and tried different code. this seems like it should work but it returns the error: “Error: t.text is not a function. (In ‘t.text()’, ‘t.text’ is undefined)” If I remove the code the error goes away.
Code:
//Notes:
// I have this import code at the top // import wixUsers from ‘wix-users’; //
// code is Inside the // $w.onReady(function () )}; //
//Check to see if the member has Student role access
let isStudent = false;
if (wixUsers.currentUser.loggedIn) {
// Get the user's member roles
wixUsers.currentUser.getRoles()
.then((roles) => {
// Check if the user has any of the Student role
if (roles.some(r => r.name === "Student")) {
isStudent = true;
} else {
isStudent = false;
}
});
}
if(isStudent) {
$w('#buttonStudent').show();
} else {
$w('#buttonStudent').hide();
}
though I have some minor coding experience I have just started trying to implement things with WIX. I was unaware of the new members APIs. I will look those up shortly.
The code is a bit Frankensitned to gather right now and not optimized as I am working on figuring out how to so things. Here is the full body of code:
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
$w.onReady(function () {
//---------------------------------------------------------------
//
// Get role restrictions Authorizaiotn
//
//---------------------------------------------------------------
let isBlackBelt = false;
//Check to see if the member has Student role access
let isStudent = false;
if (wixUsers.currentUser.loggedIn) {
// Get the user's member roles
wixUsers.currentUser.getRoles()
.then((roles) => {
// Check if the user has any of the Student role
if (roles.some(r => r.name === "Student")) {
isStudent = true;
} else {
isStudent = false;
}
});
}
if(isStudent) {
$w('#buttonStudent').show();
} else {
$w('#buttonStudent').hide();
}
if(isBlackBelt) {
$w('#buttonBlack').show();
} else {
$w('#buttonBlack').hide();
}
//---------------------------------------------------------------
//
// Refresh the page when the user logs in
//
//---------------------------------------------------------------