Max Stride Max Stride - 2 months ago 21
Javascript Question

Firebase Authentication Javascript: setCookie for pending Credential for redirect

I am trying to get the multiples auth to link using

singinwithredirect
. I grab the error
.credential
and use
JSON.stringify
to pass it through a cookie.

From there, I convert it back to an object by using
JSON.parse
, but when I tried to pass the credential object into
result.user.link(credential)
, it's giving me an error stating:

firebase.js:74 Uncaught Error: link failed: First argument "credential" must be a valid credential


Do I have to convert JSON object into a Firebase object in order to pass it in the credential? What the object originally looks like:

Ef {accessToken: "EAAQmrlAUQpEBAL4oiPWEJy.......", provider: "facebook.com"}


What the object looks like after converting back to JSON from
cookie ('string')
:

Object {accessToken: "EAAQmrlAUQpEBAL4oiPWEJy.......", provider: "facebook.com"}

Answer

You should be careful to remove cached accessToken and secret after you are done with them.

The parsed JSON is possibly not a valid firebase object to link the pending credentials. You must pass it into the firebase auth provider object first before linking.

// Read cookie
var pendingCred = document.cookie;

// Parse
var pendingCredObj = JSON.parse(pendingCred);

// Delete cookie
document.cookie ="pendingCred="pendingCred", null, 0";

// Link with appropriate auth provider object
if (pendingCredObj.provider == "twitter.com"){
    var pendingCredObjProv = firebase.auth.TwitterAuthProvider.credential(pendingCredObj.accessToken, pendingCredObj.secret);
} else if (pendingCredObj.provider == "facebook.com"){
    var pendingCredObjProv = firebase.auth.FacebookAuthProvider.credential(pendingCredObj);
} else if (pendingCredObj.provider == "google.com"){
    var pendingCredObjProv = firebase.auth.GoogleAuthProvider.credential(pendingCredObj);
} else if (pendingCredObj.provider == "password"){
    var pendingCredObjProv = firebase.auth.EmailAuthProvider.credential(pendingCredObj);
}