Assign Role To User Code

// API Reference: https://www.wix.com/velo/reference/api-overview/introduction

// “Hello, World!” Example: https://learn-code.wix.com/en/article/1-hello-world

import { assignRole } from 'backend/memberRole';

import { removeRole } from 'backend/memberRole';

 

import wixUsers from 'wix-users';

 

const currentUser = wixUsers.currentUser; //added on 13 march

var user = wixUsers.currentUser;

var userId = user.id; // "r5cme-6fem-485j-djre-4844c49"

var isLoggedIn = user.loggedIn; // true

 

var roleId;

var roleAction = "assign role";

 

$w.onReady(function () {

 

var clientRoleval = "92e87bb1-21e9-4d2d-a716-4ebc9354a73c";

$w("#txtPlanRole").text = "assign role " + clientRoleval + "to member " + userId;

 

if (roleAction === "assign role") {

assignRole(clientRoleval, userId).then(assignRoleClient => {

console.log("from front end" + assignRoleClient);

$w("#txtPlanRole").text = "api called";

})

.catch(error => {

console.log("from frontend error" + error);

});

 

}

if (roleAction === "remove role") {

assignRole("92e87bb1-21e9-4d2d-a716-4ebc9354a73c", userId).then(removeRoleClient => {

console.log(removeRoleClient);

})

.catch(error => {

console.log(error);

});

}

 

//$w("#txtPlanRole").text = planSummary;

 

});

 

 

d) Backend Script: memberRole.jsw (code is reaching till line, " console.log("backend Role api called " +roleId+ " assigned to member "+memberId );" , then throws the error.

 

 

// Filename: backend/memberRole.jsw (web modules need to have a .jsw extension)

import {roles} from 'wix-users-backend';

import wixUsersBackend from 'wix-users-backend'

 

export function assignRole(roleId, memberId) {

console.log("backend Role api called " +roleId+ " assigned to member "+memberId );

return roles.assignRole(roleId, memberId, { suppressAuth: false })

.then( () => {

console.log("Role " +roleId+ " assigned to member "+memberId );

})

.catch((error) => {

console.log("error from server "+ error);

});

}

 

export function removeRole(roleId, memberId) {

return roles.removeRole(roleId, memberId, { suppressAuth: false })

.then( () => {

console.log("Role removed from member");

})

.catch((error) => {

console.log(error);

});

}

 

https://www.wix.com/velo/forum/coding-with-velo/assignrole-api-fails-with-error-server-responded-with-message-details-401

 

 

Thanx so much Ahmad...Issue resolved.

 

 

This for the all developers....changing line in memberRole.jsw

 

from suppressAuth: false

 

to suppressAuth: true

 

 

code file for others:

 

 

// Filename: backend/memberRole.jsw (web modules need to have a .jsw extension)

import {roles} from 'wix-users-backend';

import wixUsersBackend from 'wix-users-backend'

 

export function assignRole(roleId, memberId) {

console.log("backend Role api called " +roleId+ " assigned to member "+memberId );

return roles.assignRole(roleId, memberId, { suppressAuth: true })

.then( () => {

console.log("Role " +roleId+ " assigned to member "+memberId );

})

.catch((error) => {

console.log("error from server "+ error);

});

}

 

export function removeRole(roleId, memberId) {

return roles.removeRole(roleId, memberId, { suppressAuth: true })

.then( () => {

console.log("Role removed from member");

})

.catch((error) => {

console.log(error);

});

}