wcaaan / firebase-token-authentication
为 Laravel 提供的 Firebase 令牌认证驱动程序
Requires
- php: ^7.4|^8.0
- kreait/firebase-tokens: ^1.8
- lcobucci/jwt: ^4.0
README
该驱动程序包含一个 Firebase 守卫,通过 Firebase Authentication JWT 令牌进行用户认证。要登录,请使用 Firebase Authentication。
兼容性与需求
该软件包与 php >= 7.4、Laravel 7.3 和 lcobucci/jwt 4.0 进行了测试。
安装
您可以通过 Composer 安装该软件包。
composer require wcaaan/firebase-token-authentication
更新 config/auth.php。
'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', ];
实现
更新 User 模型
更新您的认证模型。通常它是 User
use Wcaaan\FirebaseTokenAuthentication\FirebaseTokenAuthenticable
public $incrementing = false;
protected $fillable = [ 'name', 'email', 'phone', 'image' ];
Eloquent 示例
<?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 示例
<?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'
];
}
如果您使用 Eloquent,则需要手动创建或更新用户表的迁移。
$table->string('id')->primary();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('phone')->unique();
$table->string('image')->nullable();
$table->timestamps();
用法
API 守卫
您可以将 auth:api 中间件应用于一组路由或单个路由。
Route::group(['middleware' => ['auth:api']], function () { // 路由在这里 });
Route::get('/testing', function (Request $request) { return (array) $request->user(); }) ->middleware('auth:api');
对于测试,我使用了 Postman,在 Postman 中,在头部提供密钥 Authorization 和值 Bearer ... 与令牌。您从 Firebase 登录收到的对象必须具有 access_token。请用您的实际 access_token 替换 Bearer 令牌中的三个点。
Web 守卫
为了在 Web 路由中使用 Firebase 令牌认证,您必须将 Bearer 令牌附加到每个 HTTP 请求。
您也可以将 Bearer 令牌存储在 bearer_token cookie 变量中,并将其添加到您的 Kernel.php
protected $middlewareGroups = [
'web' => [
...
\Wcaaan\FirebaseTokenAuthentication\Http\Middleware\AddAccessTokenFromCookie::class,
...
],
...
];
如果您使用 EncryptCookies 中间件,您必须设置
protected $except = [
...
'bearer_token',
...
];
变更日志
有关最近更改的信息,请参阅 CHANGELOG。
贡献
此项目是从 firevel/firebase-authentication 分支的。我已经修改了它与 lcobucci/jwt 4.0 的兼容性。我还修改了实际存储库的一些行为,并添加了一个配置文件。
有关详细信息,请参阅 CONTRIBUTING。
安全
如果您发现任何安全相关的问题,请通过电子邮件 wcaaan@gmail.com 而不是使用问题跟踪器。
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。
Laravel 软件包模板
此软件包是用 Laravel 软件包模板 生成的。