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);
});
}
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
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";)
Step c is calling Backend Script: memberRole.jsw
hope this helps
regards
sneha shah
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!
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); }); }
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.