React-native-firebase: Manage error from authentification

Created on 14 May 2017  路  4Comments  路  Source: invertase/react-native-firebase

Hi,
How we can manage errors from firebase authentification ? https://firebase.google.com/docs/auth/ios/errors
When i catch error, i have this kind of message

Error: The email address is already in use by another account.
    at createErrorFromErrorData (d:\PartyStories\node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:116)

Thanks.

Most helpful comment

Hello,

The errors returned to the JS side have been converted into the same format as the firebase web sdk errors: https://firebase.google.com/docs/reference/js/firebase.auth.Auth

You just need to handle them the same way you would if you're using the web sdk.

A rough example in your case it would be something like:

// https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword
firebase.auth()
    .createUserWithEmailAndPassword(email, password)
    .catch(error => {
        switch(error.code) {
            case 'auth/email-already-in-use':
                // do something
               break;
           // handle other codes ...
       }
    });

All 4 comments

Hello,

The errors returned to the JS side have been converted into the same format as the firebase web sdk errors: https://firebase.google.com/docs/reference/js/firebase.auth.Auth

You just need to handle them the same way you would if you're using the web sdk.

A rough example in your case it would be something like:

// https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword
firebase.auth()
    .createUserWithEmailAndPassword(email, password)
    .catch(error => {
        switch(error.code) {
            case 'auth/email-already-in-use':
                // do something
               break;
           // handle other codes ...
       }
    });

Great. Thank you 馃憤

You're welcome, good luck :)

I have the same error with phone auth. how can I handle it?

` confirmPhone = async (phoneNumber) => {
        return new Promise((res, rej) => {
            firebase.auth().verifyPhoneNumber(phoneNumber)
                .on('state_changed', async (phoneAuthSnapshot) => {
                    switch (phoneAuthSnapshot.state) {
                    case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
                        await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
                        res(phoneAuthSnapshot)

                        break

                    case firebase.auth.PhoneAuthState.CODE_SENT:
                        // await userSettings.set(AUTH_KEYS.VERIFICATION_ID, phoneAuthSnapshot.verificationId)
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        res(phoneAuthSnapshot)
                        break

                    case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT: // or 'timeout'
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        res(phoneAuthSnapshot)


                    case firebase.auth.PhoneAuthState.ERROR:
                        UserStore.setErrorConfirmationCode(phoneAuthSnapshot.error)
                        rej(phoneAuthSnapshot)
                        break

                    }
                })
        })
    }

    confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
        UserStore.setCodeInput(code)
        try{
            const credential = await firebase.auth.PhoneAuthProvider.credential(UserStore.verificationId, code)
            UserStore.setUserCredentials(credential)
            AppStore.setAlreadyRegister(true)
            await this.authenticate(credential)
            console.log(credential)
            return credential
        }catch(e){
            console.log('e',e) // here I get the error
            UserStore.setErrorConfirmationCode(true)

        }
    }`
Was this page helpful?
0 / 5 - 0 ratings