PHP – Firebase auth api

Real-time data management and transactions are the latest mode of communication these days. People need quick data flow while using mobile and web applications. There are alot of services available on internet for creating real-time databases and communication systems.

register.php

<?php

header("Content-Type: application/json");
header("Acess-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization");

require 'dbcon.php';
$data = json_decode(file_get_contents("php://input"), true);

$name = $data['name'];
$email = $data['email'];
$phone = $data['phone'];
$password = $data['password'];

if($name == ""){
echo json_encode([
'status' => 200,
'data' => "Name field is required !",
]);
die();
}

if($email == ""){
echo json_encode([
'status' => 200,
'data' => "Email field is required !",
]);
die();
}

if($phone == ""){
echo json_encode([
'status' => 200,
'data' => "Phone field is required !",
]);
die();
}

if($password == ""){
echo json_encode([
'status' => 200,
'data' => "Password field is required !",
]);
die();
}

$userProperties = [
'email' => $email,
'emailVerified' => false,
'phoneNumber' => '+91'.$phone,
'password' => $password,
'displayName' => $name,
'photoUrl' => 'http://www.example.com/12345678/photo.png',
'disabled' => false,
];

$createdUser = $auth->createUser($userProperties);

if($createdUser){
echo json_encode([
'status' => 200,
'data' => $userProperties,
]);

try {
$user = $auth->getUserByEmail("$email");
$signInResult = $auth->signInWithEmailAndPassword($email, $password);
$idTokenString = $signInResult->idToken();

try {
$verifiedIdToken = $auth->verifyIdToken($idTokenString);
$uid = $verifiedIdToken->claims()->get('sub');

$data = [
'verified_token' => $verifiedIdToken,
'user_id' => $uid,
];

echo json_encode([ 
'status' => 200, 
'msg' => "Login Successfully !",
'data' => $data, 
]);

} 
catch (FailedToVerifyToken $e) {
echo "Your session has expired ! Please login again";
}


}

catch (\Kreait\Firebase\Exception\Auth\UserNotFound $e) {
echo json_encode([ 
'status' => 500, 
'msg' => "Invalid Crediential !",
]);
}

} 
else{
echo json_encode([
'status' => 500,
'data' => "Something went wrong",
]);
}


?>
api with raw data:

https://pk.pranshakti.in/firebase/register.php

Headers:
Content-Type: application/json

raw data:

  {  
    "name": "Soniya Sharma",
    "email": "soniya@gmail.com",
    "phone": "1234654897",
    "password": "123456"
  }
login.php

<?php

header("Content-Type: application/json"); 
header("Acess-Control-Allow-Origin: *"); 
header("Access-Control-Allow-Methods: POST"); 
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization");

require 'dbcon.php'; 
$data = json_decode(file_get_contents("php://input"), true);

$email = $data['email']; 
$password = $data['password'];

try {
$user = $auth->getUserByEmail("$email");
$signInResult = $auth->signInWithEmailAndPassword($email, $password);
$idTokenString = $signInResult->idToken();

try {
$verifiedIdToken = $auth->verifyIdToken($idTokenString);
$uid = $verifiedIdToken->claims()->get('sub');

$data = [
'verified_token' => $verifiedIdToken,
'user_id' => $uid,
];

echo json_encode([ 
"loginuser" => [
'status' => 200, 
'msg' => "Login Successfully !",
'data' => $data, 
] ]);

} 
catch (FailedToVerifyToken $e) {
echo "Your session has expired ! Please login again";
}


}

catch (\Kreait\Firebase\Exception\Auth\UserNotFound $e) {
echo json_encode([ 
'status' => 400, 
'msg' => "Invalid Crediential !",
]);
}

?>
api with raw data: 

https://pk.pranshakti.in/firebase/login.php 

Headers: 
Content-Type: application/json 

raw data:  

{   
  "email": "soniya@gmail.com",
  "password": "123456" 
}

Leave a Reply