I have a teaching website, I want to create some pages that are accessible to any signed up member ( students and teachers) and some pages that are restricted to specific members only ( teachers). I have created two members roles for teachers and students to prevent one of them from entering the pages designated for the other because they are in the same database (private members data), I followed the method of manually adding members to the members' roles . But I don't want to do it manually, what I want is: When a person sign up to the site, it is determined if he is a teacher or a student, Students are not allowed access to teachers pages, as well as teachers. Is there is a way to do that!
I have tried assignrole () code but to no avail, the user is not assigned a role.
reference: https://www.wix.com/velo/reference/wix-users-backend/roles-obj/assignrole
Here are my codes:
role.jsw:
import {roles} from 'wix-users-backend';
export function assignRole(roleId, memberId) {
return roles.assignRole(roleId, memberId, { suppressAuth: false })
.then( () => {
console.log("Role assigned to member");
})
.catch((error) => {
console.log(error);
});
}
register page code:
import {assignRole} from 'backend/role';
function registerTeacher() {
let email = $w("#email").value;
let password = $w("#password").value;
let first = $w("#fname").value;
let last = $w("#lastName").value;
wixUsers.register(email, password, {
contactInfo: {
"firstName": first,
"lastName": last
}
})
.then((results) => {
let roleId="be13f34e-cf4a-4f33-ae7f-c2313d824206";//Teacher role
assignRole(roleId, results.user.id);
}
});
Please any help!
solved: Only change i need to do is: suppressAuth: true
export function assignRole(roleId, memberId) {
return roles.assignRole(roleId, memberId, { suppressAuth: true })
.then( () => {
console.log("Role assigned to member");
})
.catch((error) => {
console.log("failed to assing role"+ error);
});
}