0
votes

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.

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?

Below I am posting my user model, and the migration files users and oath_clients. Feel free to ask for more code if needed!

User model:

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',
];     
}

Users migration:

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 migrations

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');
}
}
2

2 Answers

1
votes

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');
}
0
votes

It is the Qauth migration where the user_id can be null

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

either you pass this when you add the clients using for example uuid or set this as increment..