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.
let email = $w ( “#email” ). value ; let password = $w ( “#password” ). value ; let first = $w ( “#firstName” ). value ; let last = $w ( “#lastName” ). value ;
//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?
//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?
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 );
});