Laravel Passport客户端的user_id为null [英] Laravel Passport clients has user_id null
问题描述
我正在开发一个使用laravel护照后端从数据库获取和发布数据的React应用.我可以完全控制应用程序代码和laravel后端代码.
I am making a react app that uses a laravel passport backend to get and post data from a database. I have full control over the app code and the laravel backend code.
我可以使用客户端,也可以简单地通过发布用户来创建访问令牌,但是我不能使用此访问令牌来访问数据,如
I can make an access token just fine, either using a client, or simply by issuing a user to make one, but I can't use this access token to access data, as described here. Sending such a request with the generated access token gives me a 401 every time.
我相信我的问题在于结交客户时.我有2个客户端(运行php artisanpassport:install时取得),但它们都具有user_id = null.我假设当用户使用客户端(例如,发出访问令牌)时,应将其设置为一些整数?但是,事实并非如此.
I believe My problem is when making clients. I have 2 clients (made when running php artisan passport:install), but they both have user_id=null. I'm assuming this should be set some integer when a user uses the client to for example issue an access token? But, it doesn't.
那么,有人知道我在做什么错吗?
So, does anyone have any idea of what I am doing wrong?
下面,我要发布用户模型以及迁移文件用户和oath_clients.如有需要,随时询问更多代码!
Below I am posting my user model, and the migration files users and oath_clients. Feel free to ask for more code if needed!
用户模型:
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');
}
}
推荐答案
听起来像我,您需要引用用户hasOne
OAuth和OAuth belongsTo
用户的关系.将此添加到您的oauth_clients迁移中:
Sounds like to me you need to reference the relationship that a User hasOne
OAuth and a OAuth belongsTo
a User. Add this to your oauth_clients migration:
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('user');
https://laravel.com/docs/5.7/migrations#foreign -key-constraints
然后在模型中包括必要的关系.希望这会有所帮助!
Then include the necessary relationships in the Models. Hope this helps!
// 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');
}
这篇关于Laravel Passport客户端的user_id为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!