Firebase detection if user exists

How to check if user exists in Firebase auth on Signup

Button via native react?

This is my login page code:

export default class Login extends Component {
    constructor(props) {
        super(props)
        this.state = {
            email: '',
            password: '',
            response: ''
        }
        this.signUp = this.signUp.bind(this)
        this.login = this.login.bind(this)
    }

    async signUp() {
        try {
            await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
            this.setState({
                response: 'Account Created!'
            })
            setTimeout(() => {
                this.props.navigator.push({
                    id: 'App'
                })
            }, 500)
        } catch (error) {
            this.setState({
                response: error.toString()
            })
        }
    }
    async login() {
        try {
            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            this.setState({
                response: 'user login in'
            })

            setTimeout(() => {
                this.props.navigator.push({
                    id: 'App'
                })
            })

        } catch (error) {
            this.setState({
                response: error.toString()
            })
        }
    }

    render() {
        return (
            <View style={styles.container}>
                <View style={styles.containerInputes}>
                    <TextInput
                        placeholderTextColor="gray"
                        placeholder="Email"
                        style={styles.inputText}
                        onChangeText={(email) => this.setState({ email })}
                    />
                    <TextInput
                        placeholderTextColor="gray"
                        placeholder="Password"
                        style={styles.inputText}
                        password={true}
                        secureTextEntry={true}
                        onChangeText={(password) => this.setState({ password })}
                    />
                </View>
                <TouchableHighlight
                    onPress={this.login}
                    style={[styles.loginButton, styles.button]}
                >
                    <Text
                        style={styles.textButton}
                    >Login</Text>
                </TouchableHighlight>
                <TouchableHighlight
                    onPress={this.signUp}
                    style={[styles.loginButton, styles.button]}
                >
                    <Text
                        style={styles.textButton}
                    >Signup</Text>
                </TouchableHighlight>
            </View>
        )
    }
}

      

+3


source to share


2 answers


You need to use the fetchProvidersForEmail API. It receives an email and returns a promise that resolves with a list of vendors associated with that email if already registered: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#fetchProvidersForEmail



+7


source


It's very simple. Add then () and catch () method to your firebase method in the 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 about different error codes:

  • authorization / email-already in use

    Thrown if an account already exists with the specified email address.

  • Auth / invalid-mail

    Abandoned if the email address is invalid.

  • auto / operation-not-allowed

    Abandoned if email and password accounts are not enabled. Enable email / password accounts in Firebase Console under Auth tab.

  • login / weak password

    Thrown if the password is not strong enough.

+1


source







All Articles