Prevent contact duplicates in custom registration?

Hello, I have built a custom registration page as outlined here: https://support.totallycodable.com/en/article/custom-registration-for-site-login . It works as designed for brand new people and email addresses, however I am having a hard time with duplicate contact entries if someone enters an existing email address.

If a potential member enters an email address that is already in the site contacts and clicks submit, the lightbox stays up for that person as though nothing has happened but the contact is inserted into the wix contacts function anyway, creating a duplicate contact with the same email address and first/last name. It does not however create another site member, which is good.

My biggest concern is the potential member who stays stuck on the custom registration page rather than getting taken to the next location as outlined in the code. Is there a way to insert error messages into this custom registration flow? And, if so, how can I use the code outlined earlier in this thread to make that happen? Does that go inside the lightbox code or in the back-end? Please note, I already have a data.js file in the backend as outlined here to prevent duplicates on profile update: https://codequeen.wixsite.com/membership-dashboard . I am using this create/update profile flow once the user has an account on the site, and this is working well also.

For reference, my code from the custom registration lightbox is as follows:

import wixUsers from ‘wix-users’;
import wixWindow from ‘wix-window’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;

$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;

wixUsers.register(email, password, {
contactInfo: {
“firstName”: first,
“lastName”: last
}
} )
.then( (result) => {

let resultStatus = result.status;
wixWindow.lightbox.close();
wixLocation.to(“/join-wrcra-2”);
} );
} );

});

Thank you!