【问题标题】:Laravel Passport clients has user_id nullLaravel Passport 客户端的 user_id 为 null
【发布时间】:2019-07-21 01:28:33
【问题描述】:

我正在制作一个使用 laravel 护照后端从数据库获取和发布数据的反应应用程序。我可以完全控制应用程序代码和 laravel 后端代码。

我可以很好地制作访问令牌,无论是使用客户端,还是简单地通过发布用户来制作一个,但我不能使用此访问令牌来访问数据,如 here 所述。使用生成的访问令牌发送这样的请求每次都会给我一个 401。

我相信我的问题是在结交客户时。我有 2 个客户端(在运行 php artisan passport:install 时创建),但它们都有 user_id=null。我假设当用户使用客户端例如发出访问令牌时,这应该设置一些整数?但是,它没有。

那么,有没有人知道我做错了什么?

下面我发布了我的用户模型,以及迁移文件 users 和 oath_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');
}
}

【问题讨论】:

    标签: laravel laravel-passport


    【解决方案1】:

    在我看来,您需要参考用户 hasOne OAuth 和 OAuth belongsTo 用户之间的关系。将此添加到您的 oauth_clients 迁移中:

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

    https://laravel.com/docs/5.7/migrations#foreign-key-constraints

    然后在模型中包含必要的关系。希望这会有所帮助!

    // User's Table
    public function oauth()
    {
        return $this->hasOne(OAuth::class, 'user_id');
    }
    
    // OAuth Table
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
    

    【讨论】:

    • 这听起来正是我所希望的!如果您不介意,我有两个后续问题。
    • 后续问题:Passport 让一切变得非常整洁,而且非常隐蔽,所以我什至不知道应该在哪里找到 Oath 课程。通过扩展,我不知道如何访问 OAuth 模型。对不起,如果我在这里很愚蠢,但我还是 Laravel/Passport 的新手,而且框架的流畅性让我非常困惑。
    【解决方案2】:

    这是 user_id 可以为 null 的 Qauth 迁移

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

    当您使用例如 uuid 添加客户端或将其设置为增量时,您要么传递此参数..

    【讨论】:

    • 啊哈。但这不应该与使用客户端的用户相关联,还是我误解了它的作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2018-12-11
    • 2019-02-20
    • 2019-04-17
    • 2019-12-27
    • 2021-12-21
    • 2021-06-14
    相关资源
    最近更新 更多