role to be assigned

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.

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?

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.

Are you testing the trial page when logged in to the site? If not, then you don’t really have a current user.

@yisrael-wix yes i am logged in while trying this out. Please look at the console of this https://www.theindianguitarist.com/blank

@yadavveenus Well, I can look, but of course I’m not logged in. It just tells me “No user is currently logged in” (which is correct).

You can put some test login credentials in the backend file so I can login and try it.

I get this issue before. If we want to get the currentUser when the user is logged AFTER the page is loaded(maybe login with code), the currentUser returns no user is logged in. Can someone fix that?

@yisrael-wix I have modified my backend code like this

export function assignRole(roleId, memberId) {
roleId= ‘704ea06d-e0fa-4863-be1a-f8320ef2d1e6’
memberId= “32a646c8-3fdb-4910-bd79-09517ce743d0”

return roles.assignRole(roleId, memberId, { suppressAuth: false })
.then( () => {
console.log( “Role assigned to member” );
})
. catch ((error) => {
console.log(error);
});
}

I think now you will be able to see the console response without logging in.
Looking forward to your reply.

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 found a solution to the problem and I hope you will be able to explain the reason behind this:

I made two local variables in backend function that received the passed values.

export function assignRole(roleId, memberId) {
let role=roleId
let member=memberId
return roles.assignRole(role, member, { suppressAuth: true })
.then( () => {
console.log( “Role assigned to member” );
})
. catch ((error) => {
console.log(error);
});
}

Thank you for your support Yisrael. This is a very useful feature and I am glad you guys released it.

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