0
votes

I'm trying to build a site where the user can first login anonymously using firebase and then convert his account to permanent by signing in with facebook. I'm following the instructions given at https://firebase.google.com/docs/auth/web/anonymous-auth but i'm getting the following error "Uncaught ReferenceError: response is not defined". I also tried converting the account using Google Sign in but then i get the error "googleUser is not defined". What am i doing wrong?

This is my code:-

  <html>
  <body>
    <button onclick = "anonymousLogin()">Anonymous Signin</butto>
    <button onclick = "facebookSignin()">Facebook Signin</button>
    <button onclick = "facebookSignout()">Facebook Signout</button>
  </body>


 <script>
    function anonymousLogin(){
          firebase.auth().signInAnonymously().catch(function(error) {
            // Handle Errors here.
            var errorCode = error.code; console.log(errorCode)
            var errorMessage = error.message; console.log(errorMessage)
          });
     }

    function facebookSignin() {
     var provider = new firebase.auth.FacebookAuthProvider();

     var credential = firebase.auth.FacebookAuthProvider.credential(
        response.authResponse.accessToken);

     auth.currentUser.link(credential).then(function(user) {
       console.log("Anonymous account successfully upgraded", user);
      }, function(error) {
      console.log("Error upgrading anonymous account", error);
     });
    }

  </script>
</html>
2
which part of your code is generating that error? - Jaromanda X
This part var credential = firebase.auth.FacebookAuthProvider.credential( response.authResponse.accessToken); - pranit a
see how you pass response.authResponse.accessToken as the argument ... what is response? You've not shown any code that creates a variable called response - Jaromanda X
i'm not creating the variable 'response'. I've just copy pasted that code from the firebase documentation. I assume firebase processes it itself? - pranit a
the documentation isn't complete code - - Jaromanda X

2 Answers

1
votes

okay i found a solution. Apparently converting from anonymous to facebook user needs you to find the token yourself. I found this workaround.

 var provider = new firebase.auth.FacebookAuthProvider();
 firebase.auth().currentUser.linkWithPopup(provider)
0
votes

I think response here refers to the event that Facebook login passes to the callback. Facebook traditionally calls this response, but our Github sample calls it events. The name of course isn't important, but you're not declaring it for your callback.

function facebookSignin(response) {
 var provider = new firebase.auth.FacebookAuthProvider();

 var credential = firebase.auth.FacebookAuthProvider.credential(
    response.authResponse.accessToken);

Also see https://github.com/firebase/quickstart-js/blob/master/auth/facebook-credentials.html#L81