hi there
I am trying to apply assignRole() method to assign a member role to new users. I am trying it separately first on a test page with a text box and a button.
Please have a look at my page code:
import wixUsers from 'wix-users';
import {assignRole} from 'backend/studentdashboard';
let user
$w.onReady(function () {
user = wixUsers.currentUser;
let userEmail;
user.getRoles()
.then((roles)=>{
let role=""
for(var i=0;i<roles.length;i++){
role=role+roles[i].name+" "
console.log(role)
$w("#text64").text=role;
}
});
});
export function button1_click(event) {
let roleId='704ea06d-e0fa-4863-be1a-f8320ef2d1e6'
let userId=user.id;
assignRole(roleId,userId).then(resp => {
console.log(resp);
})
.catch(error => {
console.log(error);
});
}
My backend code looks like this:
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);
});
}
Please look at this. Seems like assignRole is not working. I have gone through all the community posts related to this but unable to find a solution. Any help is hugely appreciated.
Hey everybody,
I have a problem with creating a custom interaction which is supposed to assign a role (which is required to access a restricted page) to a user who just purchased a membership.
After the user gets redirected to my “thank you” page from a 3rd party provider he can have one of 3 status – logged in, registered (but not logged in) or a completely new user.
I want the user to enter his mail and password into input fields
The code then needs to check whether the user is currently
1. Logged in
2. Registered
3. A new user
Then the user would click the “get access” button. OnClick the code needs to
1. Add the specified role to the user
2. Login the user AND add the specified role to the user
3. Register the user AND add the specified role to the user
In the end, the user should be forwarded to the access only page.
I would appreciate your help as I am a total beginner with Velo. I somehow manage to get individual pieces of code to work (login, register and adding role to users). However, I cannot manage to combine them into one working code and the initial check for the user status is out of my league.
Thanks in advance
JP
Not really sure what your problem is. Perhaps either the userId or roleId is incorrect?
I created a test site. After running the site Live, I look in site Dashboard and see that the user (me for purposes of testing) has been added to the role.
The frontend code:
export function button1_click(event) { let userId = wixUsers.currentUser.id; let roleId = < the id for the role >; assignRole(roleId, userId).then(() => { console.log('done'); }) .catch(error => { console.log(error); }); }
I used a button to prevent the code from running twice in the onReady() - once server side, and then in the browser.
The backend code:
import {roles} from 'wix-users-backend'; export function assignRole(roleId, memberId) { return roles.assignRole(roleId, memberId, { suppressAuth: true }) .then( () => { console.log("Role assigned to member"); }) .catch((error) => { console.log(error); }); }
The code is pretty much the same as yours. I ran my test site, clicked the button, got the message "Role assigned to member", and then saw in the site Dashboard that the role was assigned.
I am testing in live mode on a trial page. I am expecting a new current user (with no role) to be assigned a role called "freemium" which has a specific role id when i click on #button1
in console, the value resp that is returned is displaying as undefined. When i reload the page after clicking on the button, the user still doesn't have any role assigned.
What do you mean by "seems like assignRole is not working"? Are you testing in Preview or Live? Keep in mind that (as stated in the documentation): "The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality."
What are you expecting? What are you getting?