Skip to content

Instantly share code, notes, and snippets.

@emmanuelbarturen
Created February 16, 2018 00:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emmanuelbarturen/46048c624ee79e180dfdb89943aa885e to your computer and use it in GitHub Desktop.
Save emmanuelbarturen/46048c624ee79e180dfdb89943aa885e to your computer and use it in GitHub Desktop.
Firebase rest with guzzle
<?php namespace App\Services;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
/**
* Class FirebaseService
* @package App\Services
*/
class FirebaseService
{
/**
* Register a user in the firebase authentication
* @param string $email
* @param string $password
* @return string
*/
public function signUp(string $email, string $password)
{
$data['email'] = $email;
$data['password'] = $password;
$response = $this->execute('https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser', $data);
return $response;
}
/**
* @param string $email
* @param string $password
* @return string
*/
public function signIn(string $email, string $password)
{
$data['email'] = $email;
$data['password'] = $password;
$response = $this->execute('https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword', $data);
return $response;
}
/**
* @param $url
* @param array $data
* @return null|string
*/
private function execute($url, array $data)
{
$client = new Client();
$query['query']['key'] = env('FIREBASE_API_KEY');
$query['json'] = $data;
try {
$res = $client->post($url, $query);
if ($res->getStatusCode() == 200) {
$response = $res->getBody()->getContents();
return \GuzzleHttp\json_decode($response, true);
}
} catch (ClientException $ce) {
logger($ce);
} catch (\Exception $e) {
logger($e);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment