I’m looking to create a link that goes to Page A if Member Permission is “Customer”, and Page B if Member Permission is “Partner”.
The idea being that I don’t want people who click the Partners link to see the fixed “You don’t have permission” page. Instead send those without permission to a page that invites them to become a partner, or otherwise directs them to the retail store, while of course letting the partners straight through to the partners area.
I did some research and came across an example using routers from the Stripe payment demo, but I can’t figure out how to apply this to what I’m doing, and am not using dynamic pages. I’ m very new to Corvid/JS. Hopefully will learn more as I go.
Create a backend function to get the role of currently logged-in user:
import wixUsers from 'wix-users-backend';
export function getUserRoles() {
let user = wixUsers.currentUser;
return user.getRoles()
.then((roles) => {
if (roles.length > 0) {
return roles[0].name; // "Role Name"
} else {
console.log("Member has no roles");
}
})
.catch((error) => {
console.log("Backend Error: ", error);
});
}
Import your backend function to get currently logged-in user role in frontend and use it in your front end code:
import { getUserRoles } from 'backend/userRoles'; //Assuming your function is getUserRoles and backend module is userRoles.jsw
$w.onReady(async function () {
let newRole = await getUserRoles();
//Add your code to use the role as you wish
});
Add code to redirect your site visitor to the relevant page using wixLocation API.
This is just an approach I used and you should change it to suit your case.
I really appreciate your reply. As someone very new to Corvid/Javascript, I ran into a couple of issues. I put the back end function, but then I didn’t quite know how to use the roles once that function had pulled it…
Anyhow after a lot of research today I managed to combine some info for a slightly different solution:
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixData from 'wix-data';
export function But_click(event) {
let user = wixUsers.currentUser;
user.getRoles()
.then( (roles) => {
let userRole = user.role;
if(roles.some(r => {return(r.name === "Customer")}) ===true) {wixLocation.to("/Customer-Area");}
if(roles.some(r => {return(r.name === "Partner")}) ===true) {wixLocation.to("/Partners-Area");}
})
}
This way upon clicking the Button, it sends users to the relevant link depending on their role.
Very pleased to do this… and whilst I didn’t manage from yours, it did get me totally inspired to learn enough to figure this one out, so very grateful!! =)