How to automatically assign user roles on registration?

For example: I have two member roles, student and teacher, and each role has its own registration page. How do I make it so that anybody who registers through the student registration automatically gets assigned a student role and can only see authorized pages?

1 Like

Its currently not possible to do that. You can only pass the details available in the Registration Options .

I see that people have been asking for this feature ever since 2018, its 2020 and still not introduced ??? we need it!!!

Yes I agree. Not having this option makes everything I’m trying to do so very difficult. Why can labels be automatically assigned but not roles? I cannot then assign access using only labels. Come on Wix.

I pay for a business account and this could kill a new service we are offering for online training. We NEED THIS NOW!! PLEASE PLEASE PLEASE. :pleading_face:

It’s coming. We were informed a week ago that wix-users-backend has been enhanced with assignRole and removeRole functions. No ETA, but my best guess is very soon. So hold on.

Yes! We need this right away, please. Thank you!

It’s finally out!

This is terrific news. I have a Lightbox via which Teachers register to my site. It was built for me, although I do have a little corvid experience. Does anyone have an idea how I could configure this lightbox registration so that all the teachers are assigned as Role: Teacher?

I have the same question as Nathan Fuller. I want to assign a role to an individual using an automation after they purchase a product in my store, but that has not changed.

Hey Nathan!

If only teachers complete the form using the lightbox, and it speaks to a function in your backend code that calls on wixUsersBackend.register() to create the user, then you can set up the following code after the registration has been completed.

//... Registration has just successfully completed, and returns a RegistrationResult object.

//1. You need to get the ID for your Teacher role. You can get that from your Member Permissions page.

wixUsersBackend.roles.assignRole('YOUR_ROLE_ID', RegistrationResult.user.id,{suppressAuth:true}).then(()=>{
    console.log(`Teacher ${RegistrationResult.user.id} has been added!`);
});

Please note, RegistrationResult is just a variable, and you might have it named differently in your code, which is completely fine. Whatever the name, ensure that you pull the user.id from that object.

The assignRole function returns a successful promise with no data if it’s complete.

I added some code under Nathan’s let me know if that helps. You’ll need to manually code it for now, I don’t believe there are automations through the dashboard to assign roles as yet. ( Comment under Nathan’s post )

Hi Chris! I am trying to assign a role to individuals who purchase a specific ticket on the website. Do you think you could help me? I tried using what you shared above and added in a call from Wix Events. Any help would be appreciated!

Here is what I have so far…

// Filename: backend/aModule.jsw (web modules need to have a .jsw extension)
import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
import {roles} from 'wix-users-backend';
export function wixEvents_onOrderConfirmed(roleId, memberId) {// Filename: public/Tickets.js 
 let event= ('astra-winter-camp-exhibiting-affiliates')
 return roles.assignRole('4241876b-bd88-4c22-9a12-d754cd8f04a9', memberId, {suppressAuth: false})
    .then( () => {
      console.log("WinterCamper");
    })
    .catch((error) => {
      console.log(error);
    });
}

@nicki
Hey Nicki,

The code that handles the event for wixEvents_onOrderConfirmed should be in the events.js file in your backend (backend/events.js), that’s the only way Wix can find it to trigger it. Once you place it there, let me know what happens when you trigger the event on your published site?

Hey Chris! I added it an events file in the back end and tested it on the published page. I gots nothing :rofl:

@nicki Can you share the entire set of code you used in your events.js file? Also a screenshot of the backend folder structure might help as well in diagnosing :stethoscope:

Can anyone please check for me whats wrong with my code here ? it still doesnt change the role of this member id here, i tried everything but still same problem

import wixData from ‘wix-data’ ; import wixUsersBackend from ‘wix-users-backend’ ;
import {roles} from ‘wix-users-backend’ ;

export function button1_click(event) {
assignRole( “85aef24f-4a0b-4141-b40d-da3277c871cd” , “a2a28bfb-9383-4b7b-aa32-645959661a3e” );
}

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

Is it possible to have the same functionality using active/expired pricing plans as the trigger? So when a member purchases a plan and they’re in the active period, they are assigned a role based on that plan. When they cancel their plan or their plan expires without renewal, the role would be removed?

I’m trying to do the same thing to assign roles based on the tickets sold. like gen admission = GA role vs vip pass = VIP role. I’m new to WIX and the coding side is new to me so this resolution will be extremely helpful for our business. Please share some light if you got it figured out @Chris Derrell

Same here. Been at this for a week to no avail.