yuTiAnDeV/laravel-passport-multiauth

Laravel passport 多用户认证

0.1.0 2018-03-31 09:17 UTC

This package is auto-updated.

Last update: 2024-09-06 18:49:13 UTC


README

License

Laravel Passport 添加多认证支持

安装

使用 Composer

$ composer require yutiandev/laravel-passport-multiauth

如果你使用的 Laravel 版本低于 5.5,你需要在 config/app.php 中添加提供者

    'providers' => [
        ...
        YTDev\LPM\MultiAuthServiceProvider::class,
    ]

配置

并且配置你的 config/auth.php 提供者

示例

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  => App\User::class,
            'client_id' => 2,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model'  => App\Admin::class,
            'client_id' => 4, 
        ]  
    ]

用法

请求

application/vnd.passport.provider_name 添加到 HTTP 接受头中

示例

GET /user HTTP/1.1

Host: example.com
X-Requested-With: XMLHttpRequest
Accept: application/json; application/vnd.passport.admins
Authorization: Bearer [TOKEN]

令牌

示例

public function token()
{
    $client = new GuzzleHttp\Client();
    
    $response = $http->post('http://your-app.com/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => \YTDev\LPM\Facades\PassportMultiAuth::clientId(),
            'client_secret' => 'client-secret',
            'username' => 'taylor@laravel.com',
            'password' => 'my-password',
            'scope' => '',
        ],
    ]);
    
    return json_decode((string) $response->getBody(), true);
}