firevel / firebase-authentication
Laravel 的 Firebase 身份验证驱动
2.0.0
2023-05-31 16:48 UTC
Requires
- kreait/firebase-tokens: ^4.0
- symfony/cache: ^6.2
README
一个强大的 Firebase 身份验证 API 驱动器,用于 Laravel 和 Firevel。
简介
Laravel Firebase Authentication 提供了一个复杂的 Firebase 守卫,用于通过 Firebase Authentication JWT 令牌进行用户身份验证。这允许您在 Laravel 或 Firevel 应用程序中安全地验证用户,利用 Firebase Authentication 的强大功能。Firebase Authentication。
入门指南
按照以下步骤开始使用 Laravel Firebase Authentication。
安装
首先,使用以下命令通过 composer 安装包:
composer require firevel/firebase-authentication
标准配置
- 更新
config/auth.php
:指定 Firebase 作为您应用程序的认证驱动。
'guards' => [
'web' => [
'driver' => 'firebase',
'provider' => 'users',
],
'api' => [
'driver' => 'firebase',
'provider' => 'users',
],
],
- 设置 Firebase 项目名称:通过添加
GOOGLE_CLOUD_PROJECT
到您的环境变量或设置firebase.project_id
配置变量来配置您的 Firebase 项目。 - 更新您的
User
模型:集成Firevel\FirebaseAuthentication\FirebaseAuthenticable
特性,设置$incrementing = false
并定义$fillable
。
以下是如何更新您的 User 模型以支持 Eloquent 和 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'
];
}
- 更新用户表迁移:如果您使用 Eloquent,您将需要手动创建或更新用户表的迁移。
$table->string('id');
$table->string('name');
$table->string('email')->unique();
$table->string('picture');
$table->timestamps();
微服务配置
为了避免在微服务之间共享用户数据库凭据,推荐的配置略有不同
- 更新
config/auth.php
:指定 Firebase 作为 'api' 守卫的认证驱动。
'guards' => [
...
'api' => [
'driver' => 'firebase',
'provider' => 'users',
],
],
- 更新用户提供者:在
config/auth.php
文件中,定义要使用的用户提供者,以使用Firevel\FirebaseAuthentication\FirebaseIdentity
模型。
'providers' => [
...
'users' => [
'driver' => 'eloquent',
'model' => Firevel\FirebaseAuthentication\FirebaseIdentity::class,
],
],
Web 守卫使用
要在您的 Web 路由中使用 Firebase 身份验证,需要在每个 HTTP 请求中附加 bearer 令牌。
bearer 令牌可以存储在 bearer_token
cookie 变量中。为此,请在您的 Kernel.php
中添加以下内容
protected $middlewareGroups = [
'web' => [
...
\Firevel\FirebaseAuthentication\Http\Middleware\AddAccessTokenFromCookie::class,
...
],
...
];
如果使用了 EncryptCookies 中间件,必须应用以下设置
protected $except = [
...
'bearer_token',
...
];