reCAPTCHA error in backend

I’ve set up a form to register members in my website which uses reCAPTCHA and I have an error in the backend code because of an ‘if’ clause I would like to do when the register function returns success. I don’t know where to place the ‘if’ clause, because I only want it to work in case the registration has been done correctly.

This is the code in the registration lightbox, which should work well but haven’t tried it (all suggestions are more than welcome)

import wixUsers from 'wix-users';
import wixCrm from 'wix-crm';
import wixLocation from 'wix-location';
import {processSubmission} from 'backend/submitRequest';

$w.onReady(function () {
    $w('#register').disable();
    $w('#temptxt').collapse();

    $w("#myCaptcha").onVerified(() => {
        $w('#temptxt').collapse();
        $w("#register").enable();
        let myToken = $w("#myCaptcha").token;
     });
     
     $w('#register').onClick( () => {
        let emails = [];
        let labels = [];
        let instr = "";
        
        if ($w('#firstName').valid && $w('#lastNames').valid && $w('#instrument').valid && $w('#password').valid && $w('#priv').valid && $w('#newsletter').valid) {
            if ($w('#email').valid) {
                emails.push($w('#email').value);
                
                if ($w('#instrument').value !== "no"){
                    instr = $w('#instrument').value;
                }

                if ($w('#simfonica').checked) {
                    labels.push($w('#simfonica').value);
                }
                
                if ($w('#juvenil').checked) {
                    labels.push($w('#juvenil').value);
                }
                
                let contactInfo = {
                "firstName": $w('#firstName').value,
                "lastName": $w('#lastNames').value,
                "emails": emails,
                "Instrument": instr,
                "labels": labels
                };

                let submitRequestData = {
                    "token": $w("#myCaptcha").token,
                    "data": {
                        "email": $w("#email").value,
                        "password": $w("#password").value,
                        "contactInfo": contactInfo,
                        "newsletter" : $w('#newsletter').checked
                    }
                };

                processSubmission(submitRequestData)
                    .then( () => {
                        $w("#myCaptcha").reset();
                        $w("#register").disable();
                        $w('#temptxt').html = '<span style =             "color:black"></span>';
                        $w('#temptxt').text = "T'has registrat amb èxit!";
                        $w('#temptxt').expand();
                        wixLocation.to("/inici");
                    })
                    .catch( () => {
                            $w("#myCaptcha").reset();
                            $w("#register").disable();
                            $w('#temptxt').text = "Ha hagut un error. Per favor, torna a intentar la verificació CAPTCHA.";
                            $w('#temptxt').expand();
                    })
            }
            else{
                $w('#temptxt').text = "Per favor, introdueix un email vàlid."
                $w('#temptxt').expand();
            }
        }
        else{
            $w('#temptxt').text = "Per favor, ompli correctament tots els camps."
            $w('#temptxt').expand();
        }
    });

    $w("#myCaptcha").onError(() => {
        $w('#temptxt').text = "Ha hagut un error. Per favor, torna a intentar la verificació CAPTCHA.";
        $w('#temptxt').expand();
    });
    
    $w("#myCaptcha").onTimeout(() => {
        $w("#register").disable();
        $w("#temptxt").text = "La verificació reCAPTCHA ha perdut la connexió. Per favor, torna a intentar-ho més tard.";
        $w("#temptxt").expand();
    });
});

This is the code in the backend:

// Filename: backend/submitRequest.jsw (web modules need to have a .jsw extension)
import wixCaptchaBackend from 'wix-captcha-backend';
import wixCrmBackend from 'wix-crm-backend';
import wixUsersBackend from 'wix-users-backend';

// Authorize token and insert data
export function processSubmission(submitRequestData) {
    return wixCaptchaBackend.authorize(submitRequestData.token)
        .then( () => {
            return wixUsersBackend.register(submitRequestData.data.email, submitRequestData.data.password, submitRequestData.data.contactInfo)
                .then( () => (
                    if (submitRequestData.data.newsletter) {
submitRequestData.data.contactInfo.labels.push("Subscriptors");
wixCrmBackend.createContact(submitRequestData.data.contactInfo);
};
{"type": "success"}))
                .catch( (error) => ({"type": "insertion error", "message": "Error: collection insertion failed: " + error}));
})
        .catch( (error) => ({"type": "authorization error", "message": "Error: CAPTCHA authorization failed: " + error}));
}

Hope someone can help.
Thanks in advance.

Dani.

TRY THIS

// Filename: backend/submitRequest.jsw (web modules need to have a .jsw extension)
import wixCaptchaBackend from 'wix-captcha-backend';
import wixCrmBackend from 'wix-crm-backend';
import wixUsersBackend from 'wix-users-backend';

// Authorize token and insert data
export function processSubmission(submitRequestData) {
    return wixCaptchaBackend.authorize(submitRequestData.token)
        .then( () => {
            return wixUsersBackend.register(submitRequestData.data.email, submitRequestData.data.password, submitRequestData.data.contactInfo)
                .then( () => (
                    if (submitRequestData.data.newsletter) {
submitRequestData.data.contactInfo.labels.push("Subscriptors");
wixCrmBackend.createContact(submitRequestData.data.contactInfo);
};
{"type": "success"}))
                .catch( (error) => ({"type": "insertion error", "message": "Error: collection insertion failed: " + error}));
})
        .catch( (error) => ({"type": "authorization error", "message": "Error: CAPTCHA authorization failed: " + error}));
}

Hi. Thanks for answering.

I can’t find the difference between my code and yours.
Anyway, I’ve copied and pasted your code and I get the same error:

  • Parsing error. Unexpected token if.

Maybe an if can’t go in a then? I’ve got no idea, to be honest.

This is what I have and it works

return wixCaptcha.authorize(id.token)
.then( () => {
return wixData.insert("validations", id.data)
.then( () =>
({  if(this){console.log('that')},"type": "success"}))
.catch( (error) => ({"type": "insertion error", "message": "Error: collection insertion failed: " + error}));
})
.catch( (error) => ({"type": "authorization error", "message": "Error: CAPTCHA authorization failed: " + error}));
}

Dani, in your code the second then has a parenthesis at the end of it when it should be a curly bracket.

You have it as ‘.then( () => (’ when it should be ‘.then( () => {’.

This then moves your errors down onto the line of ‘{“type”: “success”}))’

Try something like this.

// Filename: backend/submitRequest.jsw (web modules need to have a .jsw extension)
import wixCaptchaBackend from 'wix-captcha-backend';
import wixCrmBackend from 'wix-crm-backend';
import wixUsersBackend from 'wix-users-backend';

// Authorize token and insert data
export function processSubmission(submitRequestData) {
return wixCaptchaBackend.authorize(submitRequestData.token)
.then(() => {
return wixUsersBackend.register(submitRequestData.data.email, submitRequestData.data.password, submitRequestData.data.contactInfo)
.then(() => {
if (submitRequestData.data.newsletter) {
submitRequestData.data.contactInfo.labels.push("Subscriptors");
wixCrmBackend.createContact(submitRequestData.data.contactInfo);
}
return { "type": "success" }
})
.catch((error) => ({ "type": "insertion error", "message": "Error: collection insertion failed: " + error }));
})
.catch((error) => ({ "type": "authorization error", "message": "Error: CAPTCHA authorization failed: " + error }));
}