0
votes

I'm new to react native.I've created a form which handles login and register.When the user entre email and password,if the email and the password are valid,the user gets a message that he's logged in,and if the email adress doesn't exist,a new user is created.But,only creating user work,logging in isn't working

My login form:

import React, { Component } from 'react';
import { Text } from 'react-native';
import firebase from 'firebase';
import { Button, Card, CardSection, Input, Spinner } from './common';

class LoginForm extends Component{

    state={
        email:'[email protected]',
        password:'',
        error:'',
        loading:false
    };


    onButtonPress(){

        const { email, password} = this.state;
        this.setState({ error:'', loading:true });
        //Catch c'est pour gerer le cas d'echec de la requete precedante
        firebase.auth().signInWithEmailAndPassword(email, password)
        .then( this.onLoginSuccess.bind(this) )
        .catch( () => {
            firebase.auth().createUserWithEmailAndPassword(email, password)
            .then(this.onLoginSuccess.bind(this))
            .catch( this.onLoginFailed.bind(this) );
        });
    }

    onLoginSuccess(){
        this.setState({ 
            error: '', 
            loading: false,
            email : '',
            password :''
        });
        console.log("Logged succefully")
    }

    onLoginFailed(error){
        this.setState({
            error: 'Authentication Failed.', 
            loading: false
        });
        console.log(error.code);
        console.log(error.message);
    }

    renderButton(){
        if(this.state.loading){
            return <Spinner spinnerSize="small" />;
        }
        return (
            <Button onPress={this.onButtonPress.bind(this)}>Login</Button>
        )
    }

    render(){
        return (
            <Card>
                <CardSection>
                   <Input 
                   secureTextEntry={ false }
                        placeholder='[email protected]'
                        label='Email'
                        value={ this.state.email }
                        onChangeText={ emailValue => this.setState({email: emailValue}) }
                   />
                </CardSection>

                <CardSection>
                <Input 
                secureTextEntry={ true }
                    placeholder='password'
                    label='Password'
                    value={ this.state.password }
                    onChangeText={ pwdValue => this.setState({password: pwdValue}) }
                />
                </CardSection>

                <Text style={ styles.errorTextStyle }>{ this.state.error }</Text>

                <CardSection>
                    { this.renderButton() }
                </CardSection>
            </Card>
        );
    }
}

The error code that i get is : auth/network-request-failed and the error message : A network error (such as timeout, interrupted connection or unreachable host) has occurred. But i my devices has an internet connection.Adding new user works fine.

I did everything just as said in the official doc in this link : https://firebase.google.com/docs/auth/web/password-auth

1

1 Answers

0
votes

I got the same error and after little bit digging i found the reason :

emulator time issue

it's happens because emulator and network have different times. if you manually change the emulator time to be the same as network time, it will work, at least it worked for me.