简体   繁体   中英

Firebase+Javascript Convert anonymous account to permanent - Error: response is not defined

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>

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM