@mvveiga I wonder if you could also help me out with the following…
function registerUser() {
// Get user details for register or log in.
let phone = $w("#phoneInput").value;
let lastFourDigits = phone.substring(phone.length - 4, phone.length); //substring containing last 4 characters
console.log("Last 4 phone digits:",lastFourDigits);
let email = $w("#emailInput").value;
let password = "GBSC" + lastFourDigits;
let firstName = $w("#firstNameInput").value;
let lastName = $w("#lastNameInput").value;
// Find if user has previously registered; if registered: log in, if not yet registered: register and then log in.
wixData.query('soccerMembersDatabase')
.eq('email', email)
.find()
.then((results) => {
if(results) {
wixUsers.login(email, password)
.then( () => {
console.log("Existing user is logged in");
} )
.catch( (err) => {
console.log(err);
} );
} else {
wixUsers.register(email, password, {
contactInfo: {
"firstName": firstName,
"lastName": lastName,
"phones": [phone]
}
} )
.then( (result) => {
let resultStatus = result.status;
wixUsers.login(email, password)
.then( () => {
console.log("New user is logged in");
} )
.catch( (err) => {
console.log(err);
} );
} );
}
})
}
This is supposed to log in the user if their email already exists within the database or register them and then log them in if their email is not in the database.
It works for users that are already in the database but not for users that aren’t.