I am looking for a way in react-native to make user sign up with his phone number then add his email and password. But when a user logs in, he logs in only with email and password.
The phone number is only for security reasons.
I do a signInWithPhoneNumber() after user verify his number i call a createUserWithEmailAndPassword() but that's made in console Auth Two separate authentication in the same time "Email" & "Phone Number"
code
// I just pass the result into separate screen then use it to confirm all stuff is work :D
...
auth()
.signInWithPhoneNumber(phoneWithAreaCode, true)
.then(confirmResult => {
console.log('2', phoneWithAreaCode);
console.log('confirmResult', confirmResult);
this.setState({
confirmResult,
loading: false,
});
})
...
confirmCode = codeInput => {
const {confirmResult, email, password} = this.state;
console.log('codeInput Is:', codeInput.length);
if (confirmResult && codeInput.length) {
confirmResult
.confirm(codeInput)
.then(async () => {
clearInterval(this.interval);
const {params} = this.props.navigation.state;
await auth()
.createUserWithEmailAndPassword(email, password)
.then(({user}) => {
console.log('hey u');
let uid = user.uid;
params.createUser(uid);
})
.catch(error => {
// Handle Errors here.
var errorCode = error.code;
switch (errorCode) {
case 'auth/email-already-in-use':
this.setState({loading: false, password: ''});
break;
case 'auth/invalid-email':
this.setState({loading: false, password: ''});
break;
case 'auth/operation-not-allowed':
this.setState({loading: false, password: ''});
break;
case 'auth/weak-password':
this.setState({loading: false, password: ''});
break;
default:
this.setState({loading: false, password: ''});
break;
}
});
//Check if any users exist
// database()
// .ref(`users`)
// .limitToFirst(1)
// .once('value', async snapshot => {
// if (snapshot.exists()) {
// console.log('exists!');
// return true;
// } else {
// // params.createUser(user.uid);
// console.log('No user found Hah');
// }
// });
this.setState({
timer: 0,
isValid: true,
});
})
.catch(error => {
let errorCode = error.code;
let errorMessage = error.message;
console.log(errorCode);
switch (errorCode) {
case 'auth/invalid-verification-code':
this.setState({codeInput: ''});
this.refs.codeInputRef2.clear();
break;
default:
break;
}
console.log(errorMessage);
});
} else {
console.log('Not here');
}
};
EDIT
confirmCode = codeInput => {
const {confirmResult, password, email} = this.state;
console.log('codeInput Is:', codeInput.length);
if (confirmResult && codeInput.length) {
confirmResult
.confirm(codeInput)
.then(user => {
console.log(user);
let userE = auth().currentUser;
// userE.updateEmail(email);
auth().createUserWithEmailAndPassword(email, password);
clearInterval(this.interval);
const {params} = this.props.navigation.state;
params.createUser(user.uid);
this.setState({
timer: 0,
isValid: true,
});
})
.catch(error => {
let errorCode = error.code;
let errorMessage = error.message;
switch (errorCode) {
case 'auth/invalid-verification-code':
this.refs.codeInputRef2.clear();
break;
default:
break;
}
console.log(errorMessage);
});
} else {
console.log('Not here');
}
};