2

In my application I don't have user login, but I want to secure my Firebase data. I use for this Firebase Token Generator - PHP. Access to the database is like Russian roulette. Once I have permissions, once not when I'm refreshing page.

here is my token generator file:

<?php
    require_once 'vendor/autoload.php';

    use Firebase\Token\TokenException;
    use Firebase\Token\TokenGenerator;

    try {
        $generator = new TokenGenerator('Firebase-Secret');
        $token = $generator
            ->setData(array('uid' => '123'))
            ->setOption('admin', true)
            ->create();
    } catch (TokenException $e) {
        echo "Error: ".$e->getMessage();
    }

    echo $token;

?>

here is index.php:

<script>var token = "<?= include_once 'getToken.php' ?>";</script>

$(document).ready(function(){

        firebaseRef.authWithCustomToken(token, function(error, authData) {
            console.log(authData)
        }, {remember: "sessionOnly" });


            // do something
})
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Michalk
  • 23
  • 3
  • Welcome to SO. Is there an actual question in there? I don't see any question mark. Also, if you have any more info, go ahead and edit the original. http://stackoverflow.com/tour – David De Sloovere Aug 20 '15 at 14:10

1 Answers1

1

In the future, please include a minimal, complete repro for your debugging issues. Most likely, you are running into asynchronous issues. Your //do something code probably needs to move into the callback for auth.

$(document).ready(function(){
  firebaseRef.authWithCustomToken(token, function(error, authData) {
      console.log(authData);

      if( !error ) {
         // run your authenticated code here
      }

  }, {remember: "sessionOnly" });

  // trying to run something here that requires auth will
  // be non-deterministic

});
Community
  • 1
  • 1
Kato
  • 40,352
  • 6
  • 119
  • 149
  • I tried authenticate before token was generated, now I'm getting token by ajax and auth in .done() – Michalk Aug 21 '15 at 08:04