customergauge/session

Laravel Authentication 的原生 PHP Session 适配器

2.2.1 2024-06-12 11:30 UTC

This package is auto-updated.

Last update: 2024-09-12 11:58:21 UTC


README

Code Coverage Scrutinizer Code Quality

Laravel PHP Session ⛔

这个库为 Laravel 提供了一个 NativeSessionUserProvider。

安装

composer require customergauge/session

使用方法

认证配置

auth.php 文件中,添加以下设置

默认守卫

    'defaults' => [
        'guard' => 'php',
        'passwords' => 'users',
    ],

新的守卫配置

    'guards' => [
        'php' => [
            'driver' => \CustomerGauge\Session\NativeSessionGuard::class,
            'provider' => \CustomerGauge\Session\NativeSessionUserProvider::class,
            'domain' => '.app.mydomain.com',
            'storage' => 'tcp://my.redis.address:6379',
        ],
    ],

认证中间件

App\Http\Kernel 中配置 auth 中间件为 'auth:php'

用户工厂

您还需要提供自己的 UserFactory 实现并将其在 ServiceProvider 中注册。

final class NativeSessionUserFactory implements UserFactory
{
    public function make(array $session): ?Authenticatable
    {
        // $session here is the same as $_SESSION
        
        return new MyUserObject(
            $session['id'],
            $session['my_user_attribute'],
        );
    }
}

在提供者中

$this->app->bind(CustomerGauge\Session\Contracts\UserFactory, App\Auth\NativeSessionUserFactory::class);