email COULD NOT FETCH error in react-native-twitter-signin library

506 Views Asked by At

While implementing twitter signin in react native with react-native-twitter-signin I have followed all steps from the documentation but I am not receiving user object in then() method. It throws an error in catch block. The error is following

Error: { NativeMap: {"email":"COULD_NOT_FETCH","userName":"osamaaer05","userID":"13540882326114024","name":"osamaeer05","authTokenSecret":"nymvUWPAnBnvvhfhbQ1IU1ojETUuJWvq01VCiLOZDn9","authToken":"1354088232611414024-425rd6PMkdQVk1YrP1gAiiBuwpc9"} }

2

There are 2 best solutions below

0
Bhawna On

If email is not required replace the function in node_modules/@react-native-twitter-signin/twitter-signin/android/src/main/java/com/rntwittersignin/twittersignin/TwitterSigninModule.java

@Override
 public void failure(TwitterException exception) {
 map.putString("email", "");
 promise.resolve(map);
}
0
JANO On

I faced the same problem using the react-native-twitter-signin/twitter-signin to authenticate with firebase.

Similar to your error, mine also contains all necessary information (besides the email) for a successful login. So, I thought we could use the error for authentication.

As it turns out this is also proposed in the official docs published by firebase for JS. They offer a method called credentialFromError() which is also available for react-native.

With this information, I came up with the following code that lets me log in with twitter:

import auth from '@react-native-firebase/auth';
import RNTwitterSignIn from '@react-native-twitter-signin/twitter-signin';


RNTwitterSignIn.init('XXX_API_KEY_XXX', 'XXX_API_SECRET_XXX').then(() =>
    console.log('Twitter SDK initialized'),
);

async function onTwitterButtonPress() {

    var twitterCredential;

    try {
        // Perform the login request
        const { authToken, authTokenSecret } = await RNTwitterSignIn.logIn();

        // Create a Twitter credential with the tokens
        twitterCredential = auth.TwitterAuthProvider.credential(authToken, authTokenSecret);

    } catch (e) {
        // Create a Twitter credential with the error       
        twitterCredential = auth.TwitterAuthProvider.credentialFromError(e);
    }

    return auth().signInWithCredential(twitterCredential);
}

P.S.: Not sure if the question is related to firebase, but I think it is still a helpful insight that the error does not mean that the authentication/login failed.