Luigi Briganti
Luigi Briganti

Reputation: 51

Laravel Passport create clients programmatically

Is there a way to create clients from user interface in Laravel? If I need to allow users to create and manage their own clients, how can I run "passport:client" in a function context in order to create a client on the fly?

I tried by making a OauthClient model and implementing a form that generates the client, but the so created clients are not recognized in requests (they are random strings of 40 characters).

Upvotes: 3

Views: 3826

Answers (1)

suhype
suhype

Reputation: 71

I strongly recommend looking at the source code where the command is handled (and maybe this gist)

There is no need to create an own model class! You can create new clients programmatically by using the Laravel\Passport\ClientRepository class. Simply take one of these options:

Use dependency injection

You can inject the ClientRepository class into your controller/route functions. E.g. in the routes/web.php:


Route::get('test', function (\Laravel\Passport\ClientRepository $clientRepository) {
    $clientRepository->create(null, 'MyTest', 'https://example.com/auth/callback');
});

Use the app() helper

In fact also dependency injection but callable from any place of your code, you can use the app() helper:

$clientRepository = app('Laravel\Passport\ClientRepository');
$client = $clientRepository->create(null, 'MyTest', 'https://example.com/auth/callback');

Upvotes: 6

Related Questions