Laravel 8: How to create Laravel Passport OAuth2 client?

Most developers were surprised when they installed Laravel Passport on Laravel 8 – there are no Vue components for creating OAuth2 clients anymore. Most likely it happened because Laravel 8 does not use Bootstrap as the default CSS framework and using Tailwind. There are four ways to create a client with Laravel Passport:

  1. Artisan Command
  2. JSON API
  3. Copy components from Laravel 7
  4. From backend code

Create OAuth2 client with Artisan command

Laravel Passport still has Artisan command to create a client:

php artisan passport:client

It’s useful only for developers and only for creating clients not for managing them.

Manage OAuth2 clients with JSON API

A nice feature that Laravel Passport has is JSON API routes. They are guarded by auth and web middlewares and can be called only from your application. So you can create your OAuth2 client management. Here is an example of these routes:

// List clients
GET /oauth/clients

// Create client
POST /oauth/clients

// Update client
PUT /oauth/clients/{client-id}

// Delete client
DELETE /oauth/clients/{client-id}

Before using them you should add Passport::routes() to AuthServiceProvider.php boot() method:

    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }

And here is an example of creating a client with JSON API:

createClient() {
    const data = {
        name: this.clientName,
        redirect: 'https://my.redirect.url'
    };

    axios.post('/oauth/clients', data)
        .then(response => {
            console.log(response.data);
        })
        .catch (response => {
            // List errors on response...
        });
}

By the way, if you will take a look at the Laravel Passport component from Laravel 7 then you will see that there are the same JSON API routes used.

Use Laravel Passport components from Laravel 7

You can take components from Laravel 7 to create an OAuth2 client management system. Just place these components into your components folder and use them. They will not work because Laravel 8 doesn’t use Bootstrap and jQuery by default. But if you will set up Bootstrap and jQuery and change components by your needs I think it will work.

You can download these components from our gists page:

Clients.vue

AuthorizedClients.vue

PersonalAcessTokens.vue

In my opinion, manage clients with JSON API is the best way to do it. But if you still need another way – you can do it straight from backend code.

Create Laravel Passport client from backend

To do so you need to use Laravel Passports’ ClientRepository. Just type-hint it in your controller and then just use its’ methods, for example:

public function createClient(ClientRepository $clients)
{
    $client = $clients->create(
        1, 'Created from backend', 'https://my.redirect.url'
    );

    return 'Client was created successfully!<Br/>
            Client ID: ' . $client->id .'<br/>
            Client Secred: ' . $client->plainSecret;
}

OAuth2 client creation code examples

You can find code examples from this article in our GitHub repository.

See how to create an OAuth2 client in our video