Jonio
Jonio

Reputation: 1293

Passport laravel createToken Personal access client not found

After setup of passport, I have configured and created a controller to manage Register-Login- and - access to a resource for a general external post request. I do not need for a specific client. But when I try to create a token in the registration or in the login:

$tokenObj=$user->createToken('APPLICATION')->accessToken;

The error is:

RuntimeException: Personal access client not found. Please create one. in file C:\xampp7.1\htdocs\passport\vendor\laravel\passport\src\ClientRepository.php on line 94 Stack trace: 1. RuntimeException->() C:\xampp7.1\htdocs\passport\vendor\laravel\passport\src\ClientRepository.php:94 2. Laravel\Passport\ClientRepository->personalAccessClient() C:\xampp7.1\htdocs\passport\vendor\laravel\passport\src\PersonalAccessTokenFactory.php:71

How can I solve it?

Upvotes: 71

Views: 155954

Answers (10)

Ibrahim Mahmoud
Ibrahim Mahmoud

Reputation: 125

just run this command line in your project

php artisan passport:client --personal

for me it worked

Upvotes: 1

MiraTech
MiraTech

Reputation: 1302

The previous solutions mentioned do work BUT doesn't solve the issue once for all because each time I need to run the same commands ... I SOLVED this issue by including the following instruction php artisan passport:client --personal inside the DatabaseSeeder.php so each time I need to refresh my database I do seeding also and it works properly:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Artisan;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call([
            UserSeeder::class,

        ]);

        $this->command->info('Database seeded successfully.');

        $parameters = [
            '--personal' => true,
            '--name' => 'Central Panel Personal Access Client', // You can customize the client name here
        ];

        // Run the command
        Artisan::call('passport:client', $parameters);
    }
}

then run the following:

php artisan migrate:fresh --seed

Note: I run php artisan passport:install only the first time after installing the Passport package, tested on Laravel 11

Upvotes: 0

lordisp
lordisp

Reputation: 730

The Traits

There're sometimes confusions with the Traits for the User Model.

Sanctum and Passport came with the same Trait, called HasApiToken.

You may require to change the namespace in the user model from:

from

use Laravel\Sanctum\HasApiTokens;

to

use Laravel\Passport\HasApiTokens;

Testing with PAC

When implementing unit test, you may create a test to create a personal-access-client too:

In case you hash your secrets in your database, you should set Passport::$hashesClientSecrets to false in your test-cases if you need the unhashed password

use RefreshDatabase

/** @test */
public function can_create_a_personal_access_client()
{
    Passport::$hashesClientSecrets = false;

    $this->artisan(
        'passport:client', 
        ['--name' => config('app.name'), '--personal' => null]
    )->assertSuccessful();


    $this->assertDatabaseCount(PersonalAccessClient::class,1);
}

Because we use the RefreshDatabase trait, it's usefull to create a helper trait for other tests that may require a personal-access-client:

<?php

namespace Tests;

use App\Models\User;
use Illuminate\Testing\TestResponse;

trait Helper
{
    protected function createPersonalClient()
    {
        Passport::$hashesClientSecrets = false;

        $this->artisan(
            'passport:client',
            ['--name' => config('app.name'), '--personal' => null]
        );

        // use the query builder instead of the model, to retrieve the client secret
        return DB::table('oauth_clients')
            ->where('personal_access_client','=',true)
            ->first();
    }
}

Now, you can re-use the class for other PAT-Tests:

use RefreshDatabase, Helper;

/** @test */
public function can_issue_a_personal_access_token()
{
    $this->createPersonalClient();

    $user = User::factory()->create()->createToken('test');
    
    $this->assertInstanceOf(PersonalAccessTokenResult::class, $user);

    $this->assertObjectHasAttribute('accessToken', $user);
    
    $this->assertObjectHasAttribute('token', $user);
}

Upvotes: 8

JahStation
JahStation

Reputation: 917

should I call

php artisan passport:install

every time I run

php artisan:migrate

there is a way to do it properly?

Upvotes: 9

oussama benounnas
oussama benounnas

Reputation: 99

i had the same error over and over again but i didn't how to reproduce it, but i figured it out why. when you execute php artisan passport:install, it stores the two clients IDs in the database after the migrate command, but if you fresh migrate again, it's obvious you'll lose those two previous IDs, this is why this error was shown after executing the install command.

UPDATE php artisan passport:install -f will force recreating the IDs for the new migrated database.

Upvotes: 2

Mohammed Atallah
Mohammed Atallah

Reputation: 61

Simply run this command

php artisan passport:install --force

Upvotes: 6

stanley mbote
stanley mbote

Reputation: 1306

After running the command

php artisan passport:client --personal

and give you this prompt

 What should we name the personal access client? [Artisan Personal Access Client]:

don't worry just type in any name and press the enter key.

Upvotes: 17

Ruslan Skaldin
Ruslan Skaldin

Reputation: 1091

In addition to the namelivia's comment. As Laravel doc says:

Before your application can issue personal access tokens, you will need to create a personal access client. You may do this using the passport:client command with the --personal option. If you have already run the passport:install command, you do not need to run this command:

php artisan passport:client --personal

But if you did not run the command:

php artisan passport:install

You should run it first.

Upvotes: 51

Ruberandinda Patience
Ruberandinda Patience

Reputation: 3755

for me it solved by running

php artisan passport:install

because it have been happened after refreshing my database.

Upvotes: 158

namelivia
namelivia

Reputation: 2735

You have to create access clients first. It is documented here. An access client it not the same than a user token, you can have one access client and many users with different passwords and tokens.

Upvotes: 41

Related Questions