0

Working on a web app in JavaScript using Firebase authentication that needs to reach out to a remote PHP script.

The user should be able to log into the web app, then once authenticated has access to the functionality of the PHP script.

I generated a token with firebase.auth().currentUser.getToken() and successfully POSTed to the PHP server.

        firebase.auth().currentUser.getToken(true).then(function(idToken) {

        var uid = firebase.auth().currentUser.uid;


        var http = new XMLHttpRequest();
        var url = "http://localhost/json.php";
        var params = "token=" + idToken + "&uid=" + uid;
        http.open("POST", url, true);

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                alert(http.responseText);
            }
        }
        http.send(params);      

      //console.log("TOKEN: " + idToken);
    }).catch(function(error) {
      // Handle error
    });

However, I'm having some issue on the PHP side where all I need is to validate that the user is currently signed in. I tried the firebase/php-jwt library but I seem to be missing how to do a simple validation so the user can unlock the functionality of the PHP script.

Any ideas?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Alex
  • 105
  • 2
  • 10
  • There is a guide how to verify tokens: https://firebase.google.com/docs/auth/admin/verify-id-tokens – krlv Nov 29 '16 at 16:29
  • Also there is a similar question you might find useful: http://stackoverflow.com/questions/37634305/firebase-token-verification – krlv Nov 29 '16 at 16:30
  • Yeah, I've combed over that Firebase doc. It's really straight forward if you're using Node.js or Java but not so much when you have to implement a third-party library for PHP. – Alex Nov 29 '16 at 16:49
  • Hi! Ever got this solved? I'm facing the same issue. – Andres SK Apr 28 '20 at 22:12

0 Answers0