Riju Pramanik
Riju Pramanik

Reputation: 23

Passport - Create client credentials grant client programmatically

The documentation Here suggests php artisan passport:client --client for creating a client, but I want to do it using a controller, or ideally, using the native JSON apis provided by passport. Is that possible at all or will I have to override the methods in Passport::client ?

Upvotes: 1

Views: 2043

Answers (2)

garrettmills
garrettmills

Reputation: 822

Old question, but here's a 2021 answer for people finding this on Google.

I find calling Artisan commands from code unsavory, especially as @kishore-kadiyala mentioned because you get back printed output, rather than code.

Instead, you can use the Laravel\Passport\ClientRepository class to create a client directly:

use Laravel\Passport\ClientRepository;

// ... snip ...

$clients = App::make(ClientRepository::class);

// 1st param is the user_id - none for client credentials
// 2nd param is the client name
// 3rd param is the redirect URI - none for client credentials
$client = $clients->create(null, 'My Client Name', '');

Here, $client is a Laravel\Passport\Client instance directly. This is exactly how the Artisan command creates the clients anyway.

Upvotes: 6

Kishore Kadiyala
Kishore Kadiyala

Reputation: 164

You can do

Artisan::call('passport:client --client');

Look at Artisan

Upvotes: 0

Related Questions