Download the PHP package sdwru/laravel-firebase-auth-plus without Composer
On this page you can find all versions of the php package sdwru/laravel-firebase-auth-plus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sdwru/laravel-firebase-auth-plus
More information about sdwru/laravel-firebase-auth-plus
Files in sdwru/laravel-firebase-auth-plus
Package laravel-firebase-auth-plus
Short Description Secure your laravel API with Google Firebase Auth and also get a full Firebase Admin SDK
License MIT
Informations about the package laravel-firebase-auth-plus
laravel-firebase-auth-plus
Secure your laravel API with Google Firebase Auth
Adding the Middleware to your Laravel API will ensure that access is only granted by using a valid bearer token issued by Goggle Firebase Auth.
The main difference between this package and the package we forked it from is that we are using laravel-firebase as a dependency. Using laravel-firebase instead of firebase-tokens removes the need for a service provider since it is already included in laravel-firebase. Since that package depends on firebase-php, you can also use all the feature firebase-php provides.
Role middleware
This package includes optional role middleware for more granular access.
Install
laravel-firebase
Publish the laravel-firebase ServiceProvider (Provider: Kreait\Laravel\Firebase\ServiceProvider
) if not already done so.
Configure laravel-firebase according to their instructions and also explained in the official firebase documentation at this link.
Those instructions make it sound more complicated than it is. All we need to do is generate a JSON file as follows:
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- Securely store the generated JSON file and add a reference to that file in your laravel
.env
file. The following example assumes we are storing the file in the root folder of our laravel installation. Rename it to whatever you want.
How to use
There are two ways to use this.
Method 1. Lock all access without JWT token
Add the Middleware on your app/Http/Kernel.php file.
Refer to the Laravel Middleware documentation on where you can put this in your Kernel.php file and how it can be used in routes.
Method 2 (recommended) using an authentication guard.
Add the Guard to app/Providers/AuthServiceProvider.php
in the boot
method.
In config/auth.php
set your api guard driver to firebase
and the model to LaravelFirebaseAuth\User::class
Add authentication to api routes in routes/api.php
.
Example: Retrieve uid (For method #2 only) from API UserController
Example: Check if logged in and retrieve firebase user object and uid (For method #2 only) from almost anywhere inside Laravel
Role Middleware
To use this optional feature add the following to app/Http/Kernel.php
.
Add role to user example
Please note, the client needs to be issued a new token for the new role to take effect. This can happen in one of 3 ways according to the documentation. The user signs in or re-authenticates, the user session gets it's ID token refreshed after an older token expires, and ID token is force refreshed by calling currentUser.getIdToken(true)
on the client end in Javascript/Vue/React etc.
Routes
After assigning roles on the firebase end, add them to routes/api.php
.
Role references
The property where we assign roles is referred to as custom claims in the following documentation.
https://firebase.google.com/docs/auth/admin/custom-claims
https://firebase.google.com/docs/firestore/solutions/role-based-access
https://firebase-php.readthedocs.io/en/5.x/user-management.html#custom-user-claims
https://www.toptal.com/firebase/role-based-firebase-authentication
Support
Feel free to open issues and provide feedback.
All versions of laravel-firebase-auth-plus with dependencies
kreait/laravel-firebase Version ^2.0
illuminate/contracts Version ^5.8|^6.0|^7.0
illuminate/support Version ^5.8|^6.0|^7.0