护照拉拉维尔的UUID问题



我想在Laravel护照中使用UUID,而不是在我的Laravel项目中使用默认ID。

我在迁移中将所有user_idclient_id列更改为uuid,并在AppServiceProvider的注册方法中添加了Passport::ignoreMigrations()

当我通过在用户表中创建一个新的原始数据在邮递员中进行测试时,我在响应中获得了令牌,但是当我在安全路由中使用此令牌时,我总是得到响应状态:

401 Unauthorized and in the response body message:Unauthenticated.

 

我为纠正这个问题做了什么:

  1. AppServiceProviderregister函数中添加:
Passport::ignoreMigrations()
  1. 运行php artisan vendor:publish --tag=passport-migrations
  2. CreateOauthAuthCodesTable更改为
$table->uuid('id');
$table->primary('id');
$table->uuid('user_id')->index();

等等其他护照表和移民

  1. 在应用程序服务提供程序和引导方法中添加以下代码:
use LaravelPassportClient;
use RamseyUuidUuid;
Client::creating(function (Client $client) {
    $client->incrementing = false;
    $client->id = RamseyUuidUuid::uuid4()->toString();
});
Client::retrieved(function (Client $client) {
    $client->incrementing = false;
});
  1. 然后我用php artisan migrate:fresh迁移我的表(警报:此命令将删除数据库的所有当前表
  2. 关键说明是不要忘记添加
protected $primaryKey = 'Id'; // in my case I capitalize id to Id

你在这儿!

您必须将

oauth_access_tokens中的user_id更改为uuid类型,请按照以下步骤操作:

创建一个迁移以更改表oauth_access_tokens

php artisan make:migration alter_table_oauth_access_tokens

public function up()
{
    Schema::table('oauth_access_tokens', function (Blueprint $table) {
        $table->dropColumn('user_id');
    });
    Schema::table('oauth_access_tokens', function (Blueprint $table) {
        $table->uuid('user_id')->index()->nullable();
    });
}
php artisan passport:install --uuids

最新更新