I believe that the answers above only show how to search for duplicates, but they do so after the member is already signed up as they use wixUsers.currentuser. The point here is to avoid signing up the users. Also, there’s no need for a custom database, as the email is stored in the members database.
This is how I solved it:
Search for existing email in backend (might make more sense to put this in a hook, but the “add hook” interface does not provide a pre-insert hook for the membersData. I’m not sure whether there’s a reason and whether it’s possible)
// Filename: backend/reg.jsw (web modules need to have a .jsw extension)
import wixUsers from 'wix-users-backend';
import wixData from 'wix-data';
function searchForSignupDuplicateEmails(emailToCheck) {
let options = {
"suppressAuth": true,
};
return wixData.query("Members/PrivateMembersData")
.eq("loginEmail", emailToCheck)
.find(options)
.then((results) => {
return results.items.length;
})
.catch((err) => {
return Promise.reject(err);
});
}
export function doRegistration(email, password, userName) {
let tryNameLowerCase = userName.toLowerCase();
return searchForSignupDuplicateEmails(email)
.then((emailDupeCheckResult) => {
if (emailDupeCheckResult > 0) {
return Promise.reject("This email is already in use");
}
})
// else //no duplicate email or username found, proceed to do registration
.then(() => {
return wixUsers.register(email, password, {
"contactInfo": {
"emails": emails,
"labels": labels
}
})
})
.catch((err) => {
return { "succeeded": false, "returnMessage": err };
});
}
And here’s code to process that reply and display error messages in the lightbox. Note the names of the $w entities which are likely not the same as yours, and you need to replace with yours. Also, I added error messages in the lightbox which are hidden by default, and only get shown when relevant ($w(‘#emailUsedErrorText’).show() and $w(‘#errorText’).show())
import wixUsers from 'wix-users';
import wixLocation from "wix-location";
import { doRegistration } from 'backend/reg';
import wixWindow from 'wix-window'; //you might not need some of these imports
$w.onReady(function () {
$w('#submit').onClick(() => {
doRegistration($w('#email').value, $w('#password').value, $w('#inputUserName').value)
.then((result) => {
if (result.succeeded === true) {
wixLocation.to("/after-signup-location");
} else {
if (result.succeeded === false) {
if (result.returnMessage === "This email is already in use") {
$w('#emailUsedErrorText').show();
} else {
$w('#errorText').show();
}
} else {
$w('#errorText').show();
}
}
})
.catch((err) => {
$w('#errorText').show();
});
});
});