Since Wix doesn’t allow auto assigning of roles on sign up, we created our own customized sign up page using Velo editor and Wix APIs to register the user and assign him a role at sign up. However post the calling of the authentication . register() API, the .then() method does not work for this function. As a result no part of the code within the .then() block is executed and hence no role is attached to the member.
(Even tried updating the APIs from Wix User to Wix Members since Wix deprecated the Wix User module).
The code on the front end side:
authentication . register ( email , password , options )
. then (( results ) => //no code inside this block is executed
{
let roleId = ‘REQUIRED ROLE ID’ ;
console . log ( “Calling Assign Role” );
assignRoleFunc ( roleId , results . member . _id );
}
The backend module:
export function assignRoleFunc ( roleId , memberId ) {
console . log ( “inside assign role” );
return authorization . assignRole ( roleId , memberId , { suppressAuth : false })
. then (() => {
console . log ( “Role assigned to member” );
})
. catch (( error ) => {
console . error ( error );
});
}
Any help would be appreciated to let us know about the issue and possible fixes.
Let’s ignore the backend code for now since it’s being called from the then() and the then() isn’t running…
How and where are you running your frontend code? Where are you getting the email and passwords? What are the options?
Ok so in short I have set up a form that takes in email ids and password along with some other details. Data from this form is then stored into variables. The actual register() function gets called in the wixFormSubmitted event handler of the form.
Here is the entire code snippet:
import { assignRoleFunc } from ‘backend/roleModule.jsw’ ;
import { myApproveByEmailFunction } from ‘backend/roleModule.jsw’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
import { authentication , currentMember } from ‘wix-members’ ;
currentMember . getMember (). catch (). then (( obj ) => {
if ( obj != **undefined** )
{
console . log ( "Load" );
$w ( '#form1' ). hide ();
wixLocation . to ( "/" );
}
});
/**
-
Adds an event handler that fires when a visitor submits a Wix Form but before it is send to the server.
*/
export function wixForms1_wixFormSubmit ( ) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
// Add your code for this event here:
let password = $w ( ‘#input6’ ). value ;
let email = $w ( ‘#input3’ ). value ;
let obj = myApproveByEmailFunction ( email );
console . log ( obj );
if ( $w ( “#input3” ). valid && password . length >= 4 && $w ( “#input2” ). valid )
{
console . log ( “approved” );
wixForms1_wixFormSubmitted ();
}
else
{
wixForms1_wixFormSubmittedError ()
}
}
/**
-
Adds an event handler that fires when a visitor submits a Wix Form and it is successfully received by the server.
*/
export function wixForms1_wixFormSubmitted ( ) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
// Add your code for this event here:
let firstname = $w ( ‘#input5’ ). value ;
let lastname = $w ( ‘#input4’ ). value ;
let email = $w ( ‘#input3’ ). value ;
let phone = $w ( ‘#input2’ ). value ;
let password = $w ( ‘#input6’ ). value ;
let street = String ( $w ( ‘#input11’ ). value );
let city = String ( $w ( ‘#input9’ ). value );
let state = String ( $w ( ‘#input13’ ). value );
let zip = String ( $w ( ‘#input7’ ). value );
let country = String ( $w ( ‘#dropdown1’ ). value );
let address = {
street : street ,
city : city ,
country : country ,
postalCode : zip
};
let options = { contactInfo : {
“phones” : [ phone ],
“firstName” : firstname ,
“lastName” : lastname
//“address”: address
}
};
let op = authentication . register ( email , password , options )
. then (( results ) =>
{
let roleId = ‘6328c59b-cf53-4335-a6fc-daa6aac5a5d5’ ;
console . log ( “Calling Assign Role” );
assignRoleFunc ( roleId , results . member . _id );
let userEmail = email ;
console . log ( email );
wixData . query ( "MemDetails" )
. contains ( "email" , userEmail )
. find ()
. then ( ( students ) => {
console . log ( students . items );
if ( students . items . length > 0 )
{
wixData . update ( "MemDetails" , { "email" : email ,
"firstName" : firstname ,
"lastName" : lastname ,
"phoneNumber" : Number ( phone ),
"street" : street ,
"city" : city ,
"stateProvince" : state ,
"zip" : Number ( zip ),
"country" : country
});
}
**else**
{
console . log ( "new mem" );
wixData . insert ( "MemDetails" , { "email" : email ,
"firstName" : firstname ,
"lastName" : lastname ,
"phoneNumber" : Number ( phone ),
"street" : street ,
"city" : city ,
"stateProvince" : state ,
"zip" : Number ( zip ),
"country" : country
});
}
});
});
console . log ( op );
}
/**
- Adds an event handler that fires when a visitor is not able to successfully submit a Wix Form to the server.
*/
export function wixForms1_wixFormSubmittedError ( ) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
// Add your code for this event here:
$w ( “#text58” ). text = “Error, Enter valid Email and a Password greater than 4 characters” ;
}
/**
Also I have attached the page design from the editor.