How Can I Check user exists in Firebase auth in Signup
Button via react native?
This is my Login Page Code:
export de
For me fetchSignInMethodsForEmail()
only works with an email that is registered with email/password and not working for emails registered with Apple, LinkedIn or other providers.
For solution to this, I came up with following work around:
auth().signInWithEmailAndPassword(email, 'some-random-password') // Password should be really long to avoid actually logging in :)
.then((response) => {
// TODO : Avoid this block
})
.catch((error) => {
if(error.code === 'auth/wrong-password'){
// TODO : If here then it means account already exists...
}
if(error.code === 'auth/user-not-found'){
// TODO : If here then you guessed it... you can create a new account.
}
})
I'm sure there is a proper solution for this and I will update this answer when I find it.
Hope this will help someone
Here's how to use the fetchSignInMethodsForEmail
API. It returns an array. If the array is empty, it means the user has never signed up/signed in to your app with the sign in methods you have used in your app.
This is an example with Google signin.
firebase
.auth()
.signInWithPopup(provider)
.then((result) => {
let token = result.credential.accessToken;
let user = result.user;
firebase
.auth()
.fetchSignInMethodsForEmail(user.email)
.then((result) => {
console.log('result', result);
});
})
.catch((error) => {
// Handle Errors here.
}
It is very simple. Add a then() and catch() to your firebase method in signUp function.
firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(()=>{
console.log('Signup successful.');
this.setState({
response: 'Account Created!'
})
})
.catch((error)=> {
console.log(error.code);
console.log(error.message);
});
Information on different Error Codes:
auth/email-already-in-use
Thrown if there already exists an account with the given email address.
auth/invalid-email
Thrown if the email address is not valid.
auth/operation-not-allowed
Thrown if email/password accounts are not enabled. Enable email/password accounts in the Firebase Console, under the Auth tab.
auth/weak-password
Thrown if the password is not strong enough.
You need to use the fetchSignInMethodsForEmail API. It takes an email and returns a promise that resolves with the list of providers linked to that email if it is already registered: https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#fetchsigninmethodsforemail