assignRole( ) api fails with error server responded with - {\"message\":\"\",\"details\":{}} (401)"]"

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: Introduction - Velo API Reference - Wix.com
// “Hello, World!” Example: Velo Learning Center
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);
});
}

Hi there :raised_hand_with_fingers_splayed:

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

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.

Please let me know if it works


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

Glad that helped! :blush:

Hi
Im not sure where to put step c in my code
Trying to set it up similar to this however I cant seem to get any roles to be assigned to new members

Need some help
Thanks!

It is on the page where the admin used to assign the role (maybe a private page with the custom interface?)

For Successful step c, there is a prerequisite of
a) Defined a site Member (login to Member only page)
b) Defined a Role

Once done, you need to copy the roleID and mention it in step c code.
( var clientRoleval = “92e87bb1-21e9-4d2d-a716-4ebc9354a73c” :wink:
Step c is calling Backend Script: memberRole.jsw

hope this helps
regards
sneha shah