Laravel Sanctum returns 尝试访问时 500 sanctum 受保护 API
Laravel Sanctum returns 500 when trying to access sanctum protected API
我正在使用 Laravel 8.12 和 PostgreSQL。我正在尝试使用 Laravel Sanctum 作为我的 API 的身份验证。这是 sanctum 的架构:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePersonalAccessTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->uuid('id')->primary()->default(new Expression("uuid_generate_v4()"));
$table->uuidMorphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('personal_access_tokens');
}
}
这是我的用户 table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary()->default(new Expression('uuid_generate_v4()'));
$table->string('first_name', 255);
$table->string('last_name', 255);
$table->string('email', 255)->index()->unique();
$table->string('password', 100);
$table->boolean('email_verified')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
现在,当我尝试在 headers 中发送令牌时,我收到此错误:SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type uuid: \"0\" (SQL: select * from \"personal_access_tokens\" where \"personal_access_tokens\".\"id\" = 0 limit 1)
。从我的角度来看,这似乎是列类型为 uuid 的错误?
这是我用来发出请求的命令:
curl --request GET \
--url http://127.0.0.1:3001/api/user \
--header 'Accept: application/json' \
--header 'Authorization: Bearer 0|s0Vpci4xXoEJh1HMEihNd65GLvDmINILaiJr8o8e' \
--header 'Content-type: application/json'
如有任何帮助,我们将不胜感激。
创建一个包含以下内容的文件。
<?php
namespace App\Models;
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
class PersonalAccessToken extends SanctumPersonalAccessToken
{
public $incrementing = true;
protected $primaryKey = "id";
protected $keyType = "string";
}
然后我调用这个文件PersonalAccessToken
。然后在 AppServiceProvider
上,做这样的事情。
我正在调用此文件 PersonalAccessToken。然后在 AppServiceProvider 上,做这样的事情:
<?php
namespace App\Providers;
use App\Models\PersonalAccessToken;
use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
Sanctum::ignoreMigrations();
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
}
}
这将解决您的问题
我正在使用 Laravel 8.12 和 PostgreSQL。我正在尝试使用 Laravel Sanctum 作为我的 API 的身份验证。这是 sanctum 的架构:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePersonalAccessTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->uuid('id')->primary()->default(new Expression("uuid_generate_v4()"));
$table->uuidMorphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('personal_access_tokens');
}
}
这是我的用户 table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary()->default(new Expression('uuid_generate_v4()'));
$table->string('first_name', 255);
$table->string('last_name', 255);
$table->string('email', 255)->index()->unique();
$table->string('password', 100);
$table->boolean('email_verified')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
现在,当我尝试在 headers 中发送令牌时,我收到此错误:SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type uuid: \"0\" (SQL: select * from \"personal_access_tokens\" where \"personal_access_tokens\".\"id\" = 0 limit 1)
。从我的角度来看,这似乎是列类型为 uuid 的错误?
这是我用来发出请求的命令:
curl --request GET \
--url http://127.0.0.1:3001/api/user \
--header 'Accept: application/json' \
--header 'Authorization: Bearer 0|s0Vpci4xXoEJh1HMEihNd65GLvDmINILaiJr8o8e' \
--header 'Content-type: application/json'
如有任何帮助,我们将不胜感激。
创建一个包含以下内容的文件。
<?php
namespace App\Models;
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
class PersonalAccessToken extends SanctumPersonalAccessToken
{
public $incrementing = true;
protected $primaryKey = "id";
protected $keyType = "string";
}
然后我调用这个文件PersonalAccessToken
。然后在 AppServiceProvider
上,做这样的事情。
我正在调用此文件 PersonalAccessToken。然后在 AppServiceProvider 上,做这样的事情:
<?php
namespace App\Providers;
use App\Models\PersonalAccessToken;
use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
Sanctum::ignoreMigrations();
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
}
}
这将解决您的问题