Link to Page Depending on Member Permission

Hey Community,

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.

Any help will be massively appreciated!

Hello,

I have encountered a similar issue before and you can adapt the scenario below:

You need to do the following to achieve the effect you are looking for:

  1. Create roles as instructed here: Creating Member Roles ;
  2. Add members to their respective roles as instructed here: Managing Your Member Roles
  3. 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);
        });
}
  1. 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

});
  1. 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.

Good luck!

Hi Sam!

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!! =)