Here we will read, how to perform php – firebase authentication system. How to work and how to customize, everything we will read and i will provide you source code with authentication guides.
Goto Here: https://firebase-php.readthedocs.io/en/stable/user-management.html#create-a-user
https://console.firebase.google.com/u/0/project/php-firebase-oauth/authentication/providers Email/Password enable
dbcon.php: <?php require __DIR__.'/vendor/autoload.php'; use KreaitFirebaseFactory; use KreaitFirebaseAuth; $factory = (new Factory) ->withServiceAccount('php-firebase-oauth-firebase-adminsdk-sc65a-18f71c3027.json') ->withDatabaseUri('https://php-firebase-oauth-default-rtdb.firebaseio.com/'); $database = $factory->createDatabase(); $auth = $factory->createAuth(); ?>
register.php: <head> <title>PHP_FIREBASE CRUD</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> </head> <br/> <div style="padding:30px"> <?php session_start(); if (isset($_SESSION['status'])) { echo "<h4>".$_SESSION['status']."</h4>"; unset($_SESSION['status']); } ?> <form method="post" action="register-user.php"> <h4>Registration Form</h4><br/> <div> <label>Name:</label> <input type="text" name="name" value="" /> </div> <div> <label>Email:</label> <input type="email" name="email" value="" /> </div> <div> <label>Mobile:</label> <input type="number" name="phone" value="" /> </div> <div> <label>Password:</label> <input type="text" name="password" value="" /> </div> <br/> <button type="submit" name="submit">Register</button> </form> </div>
register-user.php: <?php session_start(); require 'dbcon.php'; if (isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $password = $_POST['password']; } $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) { $_SESSION['status'] = "User Successfully Registered !"; header('Location: http://localhost/firebase/auth/register.php'); } else{ $_SESSION['status'] = "User Registered Failled !"; header('Location: http://localhost/firebase/auth/register.php'); } ?>
login.php: <head> <title>PHP_FIREBASE CRUD</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> </head> <br/> <div style="padding:30px"> <?php session_start(); if (isset($_SESSION['status'])) { echo "<h4>".$_SESSION['status']."</h4>"; unset($_SESSION['status']); } ?> <form method="post" action="login-user.php"> <h4>Login Form</h4><br/> <div> <label>Email:</label> <input type="email" name="email" value="" /> </div> <div> <label>Password:</label> <input type="text" name="password" value="" /> </div> <br/> <button type="submit" name="submit">Login</button> </form> </div>
login-user.php: <?php session_start(); require 'dbcon.php'; if (isset($_POST['submit'])) { $email = $_POST['email']; $password = $_POST['password']; try { $user = $auth->getUserByEmail("$email"); $signInResult = $auth->signInWithEmailAndPassword($email, $password); $idTokenString = $signInResult->idToken(); try { $verifiedIdToken = $auth->verifyIdToken($idTokenString); $uid = $verifiedIdToken->claims()->get('sub'); $_SESSION['varified_user_id'] = $uid; $_SESSION['idTokenString'] = $idTokenString; $_SESSION['status'] = "Login Successfully !"; header('Location: http://localhost/firebase/auth/dashboard.php'); } catch (FailedToVerifyToken $e) { echo 'The token is invalid: '.$e->getMessage(); } } catch (KreaitFirebaseExceptionAuthUserNotFound $e) { $_SESSION['status'] = "No User Email Found !"; header('Location: http://localhost/firebase/auth/login.php'); } }
dashboard.php: <?php session_start(); if (isset($_SESSION['status'])) { echo "<h4>".$_SESSION['status']."</h4>"; unset($_SESSION['status']); echo "Varified User ID: ".$_SESSION['varified_user_id']; echo "Token String".$_SESSION['idTokenString']; } ?>
logout.php: <?php session_destroy(); header('Location: http://localhost/firebase/auth/login.php'); ?>