wneuteboom/firebase-authentication

Laravel的Firebase身份验证驱动程序

1.0.2 2020-10-14 13:31 UTC

This package is auto-updated.

Last update: 2024-09-14 22:21:06 UTC


README

为Laravel/WNeuteboom提供的Firebase身份验证API驱动程序。

概述

驱动程序包含一个使用Firebase Authentication JWT令牌进行用户身份验证的Firebase守卫。要登录,请使用Firebase Authentication

安装

  1. 使用composer安装包
composer require wneuteboom/firebase-authentication
  1. 更新config/auth.php配置文件。
'guards' => [
    'web' => [
        'driver' => 'firebase',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],
  1. 使用WNeuteboom\FirebaseAuthentication\FirebaseAuthenticable特质更新您的User模型。

Eloquent示例

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use WNeuteboom\FirebaseAuthentication\FirebaseAuthenticable;

class User extends Authenticatable
{
    use Notifiable, FirebaseAuthenticable;

    protected $firebaseIdColumn = "firebase_id";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'firebase_id', 'name', 'email', 'picture'
    ];
}

Firequent示例

<?php

namespace App;

use WNeuteboom\FirebaseAuthentication\FirebaseAuthenticable;
use WNeuteboom\Firequent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Model implements Authenticatable
{
    use Notifiable, FirebaseAuthenticable;

    protected $firebaseIdColumn = "firebase_id";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'picture'
    ];

}

  1. 如果您使用Eloquent,则需要手动创建或更新用户表的迁移。
$table->string('firebase_id')->unique();
$table->string('name');
$table->string('email')->unique();
$table->string('picture');
$table->timestamps();

Web守卫

为了在Web路由中使用Firebase身份验证,您必须将承载令牌附加到每个HTTP请求。

您也可以将承载令牌存储在bearer_token cookie变量中,并将其添加到您的Kernel.php文件中。

    protected $middlewareGroups = [
        'web' => [
            ...
            \WNeuteboom\FirebaseAuthentication\Http\Middleware\AddAccessTokenFromCookie::class,
            ...
        ],

        ...
    ];

如果您使用EncryptCookies中间件,则必须设置

    protected $except = [
        ...
        'bearer_token',
        ...
    ];

使用方法

将Firebase Authentication提供的常规承载令牌附加到每个API调用。