Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Firebase Mobile Number (OTP) authentication

Is there any best way to integrate Firebase mobile number (otp) authentication to Laravel ? or any package ?

like image 478
Myat Zuckerberg Avatar asked Sep 05 '25 03:09

Myat Zuckerberg


1 Answers

You can make user be authenticated by following firebase official steps to let user verify cellphone number with OTP.

If you want to access firebase from server(Laravel), you can try this package kreait/laravel-firebase and use cellphone number to get whether the user have been authenticated or not.

the code may look like this (you will get user's information if user is authenticated. Otherwise, you will get exception)

use Kreait\Firebase\Factory;

// other code

// change $keyPath to yours, do not just copy and paste
$keyPath = storage_path("app/firebase/blog-361f6-firebase-adminsdk-h5dyf-2287bc06a5.json");
$auth = (new Factory)->withServiceAccount($keyPath)->createAuth();

try {
    // change $examplePhoneNumber to yours
    $examplePhoneNumber = '+886900000000';
    $user = $auth->getUserByPhoneNumber($examplePhoneNumber);

    print_r($user);

} catch (Exception $e) {
    echo $e->getMessage();
}
like image 100
董承樺 Avatar answered Sep 07 '25 20:09

董承樺