Laravel Passport客户端的user_id为null。

7 浏览
0 Comments

Laravel Passport客户端的user_id为null。

我正在制作一个React应用程序,该应用程序使用Laravel Passport后端从数据库中获取和提交数据。我对应用程序代码和Laravel后端代码拥有完全控制权。

我可以很好地生成访问令牌,可以使用客户端或直接通过用户发出令牌,但是我无法使用此访问令牌访问数据,如此处所述。每次使用生成的访问令牌发送此请求时,都会收到401错误。

我认为我的问题出在创建客户端时。我有两个客户端(在运行php artisan passport:install时创建),但它们的user_id都为null。我猜测当用户使用客户端发出访问令牌时,应该将其设置为某个整数?但是,实际上并没有设置。

所以,有人知道我做错了什么吗?

下面是我的用户模型和迁移文件users和oauth_clients。如有需要,请随时索取更多代码!

用户模型:

class User extends Authenticatable {
    use HasApiTokens, Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'api_token', 'redcap_token', 'password',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

用户迁移:

class CreateUsersTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('api_token', 60)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::dropIfExists('users');
    }
}

Oauth迁移:

class CreateOauthClientsTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('oauth_clients', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->index()->nullable();
            $table->string('name');
            $table->string('secret', 100);
            $table->text('redirect');
            $table->boolean('personal_access_client');
            $table->boolean('password_client');
            $table->boolean('revoked');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::dropIfExists('oauth_clients');
    }
}

0
0 Comments

Laravel Passport clients has user_id null问题的原因是在Qauth迁移中,user_id可以为null。解决方法是在添加客户端时传递user_id,例如使用uuid,或将其设置为自增。

$table->integer('user_id')->index()->nullable();

这个问题的解决方法是将user_id与使用客户端的用户绑定。

0
0 Comments

问题的原因是Laravel Passport的clients表中的user_id字段为空。解决方法是在oauth_clients迁移文件中添加外键约束,并在User模型和OAuth模型中添加必要的关联关系。

在oauth_clients迁移文件中添加以下代码:

$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('user');

在User模型中添加以下代码:

public function oauth()
{
    return $this->hasOne(OAuth::class, 'user_id');
}

在OAuth模型中添加以下代码:

public function user()
{
    return $this->belongsTo(User::class, 'user_id');
}

这样就可以解决Laravel Passport clients表中user_id为空的问题了。

如果你不知道如何找到Oath类以及如何访问OAuth模型,可以参考Laravel/Passport文档或者查看相关的代码文件。Laravel/Passport框架的设计可能会让你感到困惑,但随着熟悉的程度的提高,你会逐渐理解和掌握它的使用方法。

0