title: Firebase PHP CURL Authentication
body: <p>I currently building a small chat using Firebase and PHP. This, I thought, would be a good learning project for Firebase, and so far I am very happy with it!</p>
  
  <p>However, I have hit a wall. I am not sure how I can implement an authentication system to Firebase via PHP. It's quite specific what I need the authentication system to do:</p>
  
  <p>To be able to use the chat, the user must login using my custom php login system. Then once they are logged in, they will also authenticate to be able to read/write in the chat.</p>
  
  <p>I couldn't really understand how this (if even) is possible with PHP, using CURL.</p>
  
  <p>In my <code>__construct</code> function in have the following:</p>
  
  <pre><code>require('/libs/FirebaseLib.php');
  $this-&gt;firebase = new fireBase('https://&lt;url&gt;.firebaseio.com');
  
  require('/libs/JWT.php');
  require('/libs/FirebaseToken.php');
  $tokenGen = new Services_FirebaseTokenGenerator('&lt;firebase-secret&gt;');
  $this-&gt;authtoken = $tokenGen-&gt;createToken(
      array(
          'id' =&gt; $this-&gt;userid
      )
  );
  </code></pre>
  
  <p>How would I authenticate with Firebase to let the user be able to read/write in my chat and not allow non authenticated user to read/write?
  Note: I have not done anything to the Firebase security rules - this is part of my question.</p>
  
  <p>I've looked at the documentation, I might just be very thick, but I couldn't really find what I was looking for.</p>
  
  <p>Hope anyone to point me in the right direction.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong> I have intentionally not been using javascript for my chat, apart from ajax calls to my php script which then relays it to Firebase after I have done what I want to do with the user's messages.</p>
  
  <p><strong>EDIT 2:</strong> Added links to the used libraries: "<a href="https://github.com/firebase/firebase-token-generator-php" rel="nofollow noreferrer">Firebase Token Generator</a>" and "<a href="https://github.com/ktamas77/firebase-php/blob/master/firebaseTest.php" rel="nofollow noreferrer">Firebase PHP Client</a>"</p>
  
  <p><strong>EDIT 3:</strong> My current code looks like this: (<a href="https://stackoverflow.com/questions/14073328/firebase-get-data-via-rest-api-php-curl">reference</a>)</p>
  
  <p><strong>__construct:</strong></p>
  
  <pre><code>$this-&gt;authtoken = JWT::encode(
      array(
          'admin' =&gt; true,
          'debug' =&gt; true,
          'v' =&gt; 0,
          'iat' =&gt; time(),
          'd' =&gt; array('user' =&gt; 'admin')
      ),
      '&lt;secret&gt;',
      'HS256'
  );
  </code></pre>
  
  <p><strong>New Message Function:</strong></p>
  
  <pre><code>$response = $this-&gt;firebase-&gt;set('/chat.json?auth=' . $this-&gt;authtoken, array(
      'message' =&gt; array(
          'username' =&gt; 'Test',
          'time' =&gt; time(),
          'string' =&gt; 'Hello World!'
      )
  ));
  </code></pre>
  
  <p>However it returns: <code>{ "error" : "invalid_token: Could not parse auth token." }</code>. I basically want to get permission as the administrator. I have tried just using the Firebase secret as the auth, but its returns the same error.</p>
  
  <p><strong>Rules:</strong></p>
  
  <pre><code>{
    "rules": {
      ".read": "auth.username == 'admin'",
      ".write": "auth.username == 'admin'"
    }
  }
  </code></pre>