The driver contains a firebase guard that authenticates user by Firebase Authentication JWT token. To login use Firebase Authentication.
The package is tested with
php >= 7.4
Laravel 7.3
lcobucci/jwt 4.0
You can install the package via composer:
composer require wcaaan/firebase-token-authentication
'guards' => [
'web' => [
'driver' => 'firebase',
'provider' => 'users',
],
'api' => [
'driver' => 'firebase',
'provider' => 'users',
],
],
php artisan vendor:publish --provider="Wcaaan\FirebaseTokenAuthentication\FirebaseTokenAuthenticationServiceProvider" --tag="config"
return [
/*
* The firebase_project_id key is used when connecting to firebase authentication.
*/
'firebase_project_id' => '',
/*
* The target_provider key is used for connecting with your desired model.
* by default laravel provider is users
* If target_provider is not set, by defalt users will be used.
* Example: In below example your target_provider is users
*
* 'providers' => [
* 'users' => [
* 'driver' => 'eloquent',
* 'model' => App\User::class,
* ],
* ],
*
*/
'target_provider' => 'users',
];
Update your authentication model. Generally it is User
use Wcaaan\FirebaseTokenAuthentication\FirebaseTokenAuthenticable
public $incrementing = false;
protected $fillable = [ 'name', 'email', 'phone', 'image' ];
Eloquent example:
<?php
namespace App;
use Wcaaan\FirebaseTokenAuthentication\FirebaseTokenAuthenticable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable, FirebaseTokenAuthenticable;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'phone', 'image'
];
}
Firequent example:
<?php
namespace App;
use Wcaaan\FirebaseTokenAuthentication\FirebaseTokenAuthenticable;
use Firevel\Firequent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Model implements Authenticatable
{
use Notifiable, FirebaseTokenAuthenticable;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'phone', 'image'
];
}
$table->string('id')->primary();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('phone')->unique();
$table->string('image')->nullable();
$table->timestamps();
Your can apply auth:api
middleware to group of routes or single route.
Route::group(['middleware' => ['auth:api']], function () { // routes goes here });
Route::get('/testing', function (Request $request) { return (array) $request->user(); }) ->middleware('auth:api');
For testing i have used postman, in postman header provide key Authorization
and value Bearer ...
with token.
The obeject you receive from firebase login must have access_token. Please replace the three dots in Bearer token with your actual access_token.
In order to use firebase token authentication in web routes you must attach bearer token to each http request.
You can also store bearer token in bearer_token
cookie variable and add to your Kernel.php
:
protected $middlewareGroups = [
'web' => [
...
\Wcaaan\FirebaseTokenAuthentication\Http\Middleware\AddAccessTokenFromCookie::class,
...
],
...
];
If you are using EncryptCookies
middleware you must set:
protected $except = [
...
'bearer_token',
...
];
Please see CHANGELOG for more information what has changed recently.
This project is forked from firevel/firebase-authentication. I have modified it to make it compatible with lcobucci/jwt 4.0
I have also modified some of the behaviour of actual repository and added a configuration file.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email wcaaan@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.