My validation process involves a multi-function where I don't invoke a master function beforehand to check and validate inputs. Within my "SignUp Function", I have implemented a validation function as variables that are checked before passing to the remaining functions. While the validation process works smoothly, I encounter issues with the rest of the functions not being invoked resulting in an empty database.
// Example of Validation Function
handleUsername = () => {
const {userName} = this.state;
if (userName.length <= 0) {
this.setState({
NameValid: 'Please enter your name',
});
return;
} else {
this.setState({
NameValid: '',
});
}
}
signUpFunc = async () => {
console.log('I am here');
const {email, password} = this.state;
// Performing Validations on Inputs
const nameValid = this.handleUsername();
const emailValid = this.handleEmail();
const phoneValid = this.handlePhone();
const passwordValid = this.handlePassword();
if (!nameValid || !phoneValid || !emailValid || !passwordValid) {
console.log('Validation statement present');
return;
} else {
console.log('else statement'); // This is not showing up in the console after validation
console.log('email', email);
console.log('password', password);
await auth()
.createUserWithEmailAndPassword(email, password)
.then(() => {
console.log('done');
this.setState({loading: true}, () => this.createUserDatabase());
})
.catch(error => {
// Handling Errors
var errorCode = error.code;
switch (errorCode) {
case 'auth/email-already-in-use':
alert('This email is already in use, please try another one');
this.setState({loading: false, password: ''});
break;
case 'auth/invalid-email':
alert('Invalid email address, please try another one');
this.setState({loading: false, password: ''});
break;
case 'auth/operation-not-allowed':
alert('This email service is disabled by the app administration');
this.setState({loading: false, password: ''});
break;
case 'auth/weak-password':
alert('Weak password provided');
this.setState({loading: false, password: ''});
break;
default:
alert('Check your internet connection');
this.setState({loading: false, password: ''});
break;
}
});
}
};