yaquawa/laravel-passport-binary-uuid-adapter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yaquawa/laravel-passport-binary-uuid-adapter

Composer 安装命令:

composer require yaquawa/laravel-passport-binary-uuid-adapter

包简介

This package enables Laravel Passport to utilize binary UUID.

README 文档

README

By default Passport does not support using binary UUID.

This package acts as an adapter to allow Passport to be able to use binary UUID.

Notice: this package assumes you are using ramsey/uuid to generate UUID.

Usage

First, pull in the package through Composer.

composer require yaquawa/laravel-passport-binary-uuid-adapter

This package is going to bind some classes of Passport before Passport make them.

So to make it works, this package must be loaded before Passport's service provider.

To guarantee this package's service provider to be loaded before Passport's service provider, in the extra section of your application's composer.json file, add passport to the dont-discover list.

"extra": {
    "laravel": {
        "dont-discover": [
            "laravel/passport"
        ]
    }
}

Then manually add Laravel\Passport\PassportServiceProvider::class to providers in config/app.php.

Lastly, change your user provider to eloquent_with_binary_uuid in your config/auth.php.

'providers' => [
    'users' => [
        'driver' => 'eloquent_with_binary_uuid',
        'model'  => App\Models\User::class,
    ]
]

Furthermore, you can specify an array with model, in that case, the user provider will attempt to retrieve the user from different tables. This is useful when you want to use the same endpoint for a variety of users. For example, if you give both User and Admin model class to the model key, it'll first try to find a User from its table, if it can't then try to find one from Admin 's table using the same user credentials from the incoming request.

Exception Handling

Also, you should override the context method of Illuminate\Foundation\Exceptions\Handler in your App\Exceptions\Handler class. So the userId can be outputted as a normal UUID.

namespace App\Exceptions;

use Exception;
use Illuminate\Support\Facades\Auth;
use Yaquawa\Laravel\PassportBinaryUuidAdapter\Helper;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * Get the default context variables for logging.
     *
     * @return array
     */
    protected function context()
    {
        try {
            $userId = Auth::id();

            if ($userId) {
                $userId = Helper::decodeUuid($userId);
            }

            return array_filter([
                'userId' => $userId,
                'email'  => Auth::user() ? Auth::user()->email : null,
            ]);
        } catch (Throwable $e) {
            return [];
        }
    }
}

Contributing

I'm not sure if Laravel is going to allow us to use binary UUID in the future. So I'll try to align this package to the latest version of Laravel and Passport as soon as I can. Please if you discover any issues, feel free to send me a PR 🤝.

统计信息

  • 总下载量: 356
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固