firevel / firebase-authentication
Firebase authentication driver for Laravel
Installs: 18 458
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 2
Forks: 6
Open Issues: 1
Requires
- kreait/firebase-tokens: ^4.0
- symfony/cache: ^6.2
README
A robust Firebase Authentication API driver for Laravel and Firevel.
Introduction
Laravel Firebase Authentication provides a sophisticated firebase guard for user authentication via the Firebase Authentication JWT token. This allows you to securely authenticate users in your Laravel or Firevel applications, leveraging the power of Firebase Authentication.
Getting Started
Follow these steps to get started with Laravel Firebase Authentication.
Installation
Begin by installing the package using composer with the command:
composer require firevel/firebase-authentication
Standard Configuration
- Update
config/auth.php
: Specify Firebase as the authentication driver for your application.
'guards' => [
'web' => [
'driver' => 'firebase',
'provider' => 'users',
],
'api' => [
'driver' => 'firebase',
'provider' => 'users',
],
],
- Set Firebase project name: Configure your firebase project by adding
GOOGLE_CLOUD_PROJECT
to your environment variables or set thefirebase.project_id
config variable. - Update your
User
model: IntegrateFirevel\FirebaseAuthentication\FirebaseAuthenticable
trait, set$incrementing = false
and define$fillable
.
Below are examples of how to update your User model for Eloquent and Firequent:
Eloquent
<?php
namespace App;
use Firevel\FirebaseAuthentication\FirebaseAuthenticable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable, FirebaseAuthenticable;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'picture'
];
}
Firequent
<?php
namespace App;
use Firevel\FirebaseAuthentication\FirebaseAuthenticable;
use Firevel\Firequent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Model implements Authenticatable
{
use Notifiable, FirebaseAuthenticable;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'picture'
];
}
- Update Migration for users table: If you're using Eloquent, you'll need to manually create or update the migration for the users table.
$table->string('id');
$table->string('name');
$table->string('email')->unique();
$table->string('picture');
$table->timestamps();
Micro-service Configuration
To avoid sharing users database credentials between micro-services, the recommended configuration differs slightly:
- Update
config/auth.php
: Specify Firebase as the authentication driver for the 'api' guard.
'guards' => [
...
'api' => [
'driver' => 'firebase',
'provider' => 'users',
],
],
- Update User Provider: In the
config/auth.php
file, define the user provider to use theFirevel\FirebaseAuthentication\FirebaseIdentity
model.
'providers' => [
...
'users' => [
'driver' => 'eloquent',
'model' => Firevel\FirebaseAuthentication\FirebaseIdentity::class,
],
],
Web Guard Usage
To utilize firebase authentication within your web routes, it is necessary to attach the bearer token to each HTTP request.
The bearer token can be stored in the bearer_token
cookie variable. To do this, add the following to your Kernel.php
:
protected $middlewareGroups = [
'web' => [
...
\Firevel\FirebaseAuthentication\Http\Middleware\AddAccessTokenFromCookie::class,
...
],
...
];
If the EncryptCookies middleware is in use, the following settings must be applied:
protected $except = [
...
'bearer_token',
...
];