Assign Roles depending on Radio button

Hi Guys,

I’m creating custom registration form where I want to automatically assign a role depending on the radio button option chosen. I was able to assign just one role for every person who registered but I now want to assign different roles depending on the option chosen.
Can someone please check my last part of my code below?

. then (( registrationResult ) => {
if ( $w ( “#radioGroup1” ). selectedIndex === 0 ){
let roleId = “7f46f5fd-8f80-4712-9c52-4a46eaf4263a” ;
assignRole ( roleId , registrationResult . member . _id );}
else if ( $w ( “#radioGroup1” ). selectedIndex === 1 ){
let roleId = “3c3dedc0-e5a4-4407-aafb-7299c5eec789” ;
assignRole ( roleId , registrationResult . member . _id );}
})
})})

The part of code you posted can work fine (if the other part you haven’t posted is ok). But I’d add a return before (both) assignRole calls and see if it returns the expected result.

I added the returns but still no change. Below my full code:

import { authentication } from ‘wix-members’ ;
import { assignRole } from ‘backend/assignRole.jsw’ ;

$w . onReady ( function () {

$w ( "#registerButton" ). onClick ( ( event ) => { 

let email = $w ( “#email” ). value ;
let password = $w ( “#password” ). value ;
let first = $w ( “#firstName” ). value ;
let last = $w ( “#lastName” ). value ;

authentication . register ( email , password , {
contactInfo : {
“firstName” : first ,
“lastName” : last
}
} )
. then (( registrationResult ) => {
if ( $w ( “#radioGroup1” ). selectedIndex === 0 ){
let roleId = “7f46f5fd-8f80-4712-9c52-4a46eaf4263a” ;
assignRole ( roleId , registrationResult . member . _id ); {
return assignRole . assignRole ( roleId , registrationResult . member . _id , { suppressAuth : false })
. then ( () => {
console . log ( “Role assigned to member” );
})
. catch (( error ) => {
console . log ( error );
});
}}

**else if** ( $w ( "#radioGroup1" ). selectedIndex  ===  1 ){ 
**let**  roleId = "3c3dedc0-e5a4-4407-aafb-7299c5eec789" ; 
assignRole ( roleId ,  registrationResult . member . _id );{  **return**  assignRole . assignRole ( roleId ,  registrationResult . member . _id , {  suppressAuth :  **false**  }) 
. then ( () => { 
  console . log ( "Role assigned to member" ); 
}) 
. **catch** (( error ) => { 
  console . log ( error ); 
}); 

}}})
})})

//front end
// ̶i̶m̶p̶o̶r̶t̶ ̶{̶ ̶a̶u̶t̶h̶e̶n̶t̶i̶c̶a̶t̶i̶o̶n̶ ̶}̶ ̶f̶r̶o̶m̶ ̶'̶w̶i̶x̶-̶m̶e̶m̶b̶e̶r̶s̶'̶;̶
import { register} from 'backend/registration.jsw';

$w.onReady(function () {
$w("#registerButton").onClick( (event) => {
//to be sure all the fields are filled in:
if(!["email", "password", "firstName", "lastName"].every(e => $w("#" + e).valid)){
return $w("#fillInAllFieldsMsg").show();
} else {
$w("#fillInAllFieldsMsg").hide();
} //
const [email, password, first, last] = [$w("#email").value, $w("#password").value, $w("#firstName").value, 
$w("#lastName").value];
 register(email, password, {
       contactInfo: {
        "firstName": first,
        "lastName": last
       }
      },  $w("#radioGroup1").selectedIndex)
    .then(() => {
      console.log("Role assigned to member");
    })
    .catch((error) => {
      console.log(error);
    });
})
})
//backend/registration.jsw
import {authentication, authorization} from 'wix-members-backend';
export function register(email, password, info, roleIndex){
return authentication.register(email, password, info)
.then(({member}) => {
  const roleIds = [
"7f46f5fd-8f80-4712-9c52-4a46eaf4263a",
"3c3dedc0-e5a4-4407-aafb-7299c5eec789"
];//order it by the same index of your radio button indexes
return authorization.assignRole(roleIds[roleIndex], member._id, {suppressAuth:true})
    }).catch(err => err)
}

It works, thank you!
Could also help me with the following please?
Can i redirect the newly created member to a different member area depending on their radio button choice?

Sure:

//front-end:
import wixLocation from "wix-location";
import { register} from "backend/registration.jsw";

$w.onReady(function () {
$w("#registerButton").onClick( (event) => {
//to be sure all the fields are filled in:
if(!["email", "password", "firstName", "lastName"].every(e => $w("#" + e).valid)){
return $w("#fillInAllFieldsMsg").show();
} else {
$w("#fillInAllFieldsMsg").hide();
} //
const [email, password, first, last] = [$w("#email").value, $w("#password").value, $w("#firstName").value, 
$w("#lastName").value];
 register(email, password, {
       contactInfo: {
        "firstName": first,
        "lastName": last
       }
      },  $w("#radioGroup1").selectedIndex)
    .then(() => {
       const targetPath = $w("#radioGroup1").selectedIndex ? "/path1" : "/path0";//use the path to the page you want. The first one is for index 1, the second for index 0
      wixLocation.to(targetPath);
    })
    .catch((error) => {
     console.log("error", error);
    });
})
})

Thank you, your replies are really helpful. Actually, there are 3 different radio button choices. I manage to add them to my roles but what do I need to change in the code for directing to other page please?

instead of these lines:

const targetPath =$w("#radioGroup1").selectedIndex ?"/path1":"/path0";
wixLocation.to(targetPath);

Use:

const selectedValue = $w("#radioGroup1").value;
let targetPath = '/';
switch(selectedValue){
case 'dog':/*use the value you set for the radio button. 'dog' is just an example*/
targetPath += 'path1';
break;
case 'cat';
targetPath += 'path2';
break;
case 'parrot';
targetPath += 'path3';
break;
}
wixLocation.to(targetPath);

(Alternatively you can use indexes instead of values, but I think it’s better to use values as you may change the order in the future)

Thank you! another 2 questions:
Question 1: It seems that after registration the member is created but is not logged in. Do I need to include a session token? If yes, how would you do it? I’m stuck for this part. The code doesn’t recognize the “sessionToken”.

Question 2:
the location i want to direct to is a dynamic Page. I’m not sure if I did it correct in my coding due to the problem in question 1.

Below my code:
import wixLocation from “wix-location” ;
import { register } from “backend/registration.jsw” ;
import { myGenerateSessionTokenFunction } from “backend/Sessiontoken.jsw” ;
import { authentication } from ‘wix-members’ ;

$w . onReady ( function () {
$w ( “#registerButton” ). onClick ( ( event ) => {
//to be sure all the fields are filled in:
if (![ “email” , “password” , “firstName” , “lastName” ]. every ( e => $w ( “#” + e ). valid )){
return $w ( “#fillInAllFieldsMsg” ). show ();
} else {
$w ( “#fillInAllFieldsMsg” ). hide ();
} //
const [ email , password , first , last ] = [ $w ( “#email” ). value , $w ( “#password” ). value , $w ( “#firstName” ). value ,
$w ( “#lastName” ). value ];
register ( email , password , {
contactInfo : {
“firstName” : first ,
“lastName” : last
}
}, $w ( “#radioGroup1” ). selectedIndex )
. then (() => {
const selectedValue = $w ( “#radioGroup1” ). value ;
let targetPath = ‘/’ ;
switch ( selectedValue ){
case ‘Dronepiloot’:/* use the value you set for the radio button . ‘dog’ is just an example */
targetPath += ‘dronepiloot-area’ ;
break ;
case ‘Klant’ :
targetPath += ‘/registratie-opdrachtgever/$w{E-mailadres }’ ;
break ;
case ‘Partner’ :
targetPath += ‘ik-wil-partner-worden’ ;
break ;
}
wixLocation . to ( targetPath );
})
. catch (( error ) => {
console . log ( “error” , error );
});

authentication . applySessionToken ( sessionToken ) 

. then (() => {
console . log ( ‘Member logged in.’ );
});})
})

Question 1 is solved, only question 2 still open:
I want to redirect case ‘Klant’ to a dynamic page but I do something wrong.