Actual full roles assigned

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);

});

}

 

 

This is a simple and straight forward api call, but surprisingly failing to assign role and Im getting this error .Any pointers will be a big help, it looks like Im missing a small step....

 

 

 

 

 

 

what have I done:

 

a) Defined a site Member

 

b) Defined a Role

 

 

 

 

 

 

 

 

 

 

c) Defined Velo code to call assignRole api

 

 

// 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);

});

}

 

 

 

 

 

 

 

Answer

 

0

 

Share

 

2

 

Ahmad

Dash ·

Content Creator

·

Mar 16

·

Edited:

Mar 16

 

Hi there 🖐

 

 

The error means that you're not authorized to perform this action because you're not an admin and you haven't set the authentication suppression option to true.

 

 

In your code you're setting it to false, but why? It's false by default! You should set it as true, that might solve your issue.

 

 

Hope this helps~!

 

Ahmad

 

 

Reply

 

1

 

Sneha Shah

Mar 16

 

omg...so quick response...kudos to you sir.

 

I just copied the code from the api link and was wondering if I shall make the authorization to true...

 

 

https://www.wix.com/velo/reference/wix-users-backend/roles-obj/assignrole

 

 

I'll try with this change and will update.

 

 

Reply

Ahmad

Dash ·

Content Creator

Mar 16

 

Please let me know if it works

 

 

Reply

 

0

 

1

 

Sneha Shah

Mar 16

 

 

 

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