Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase signInWithCredential failed: First argument "credential" must be a valid credential

I am using the react-native-facebook-login package to log users in. Currently the flow is working well and after the user enters their details, I successfully see an object returned with their information.

When I try and create an account in Firebase with signInWithCredential, I receive the following error message:

signInWithCredential failed: First argument "credential" must be a valid 

I can't seem to find a breakdown of how that credential needs to be passed - is it a string, an object, an array etc. Is it just the token or do I need to pass other details (i.e. the provider)?

The credentials object I am currently getting back has:

  • permission: Array
  • token: String
  • tokenExpirationDate: String
  • userId: String

Any help would be much appreciated - thanks!

like image 533
william Avatar asked Jan 14 '17 15:01

william


2 Answers

Feeling pretty pleased - finally cracked the nut.

They key bit is the token needs to be changed first before being a relevant credential. See code below:

onLogin={function(data){
    let token = firebase.auth.FacebookAuthProvider.credential(data.credentials.token);
    firebase.auth().signInWithCredential(token)
       .then((user) => {
           console.log(user)
       }).catch((err) => {
           console.error('User signin error', err);
    });
}}
like image 144
william Avatar answered Oct 16 '22 21:10

william


to answer your question, based on the documentation of firebase: where GoogleAuthProvider could be any of your setup / supported auth providers

// Build Firebase credential with the Google ID token.
var credential = firebase.auth.GoogleAuthProvider.credential(id_token);

// Sign in with credential from the Google user.
firebase.auth().signInWithCredential(credential).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
});

On a side note, as you are using react-native and firebase, did you already try react-native-firestack? makes a lot of things easier.

like image 26
funkysoul Avatar answered Oct 16 '22 22:10

funkysoul