Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I generate a client ID and client secret in Laravel Passport
    Laravel Passport generate client ID client secret API authentication client credentials grant Laravel API Passport client setup


How do I generate a client ID and client secret in Laravel Passport


To generate a client ID and client secret in Laravel Passport, you can use the `passport:client` Artisan command. Here are the steps:

1. Install Passport:
- Run the `install:api` command to install Passport and create the necessary database tables:

bash
     php artisan install:api --passport
     

2. Create a Client:
- Run the `passport:client` command to create a new client. This command will prompt you for details about your client and provide a client ID and client secret:

bash
     php artisan passport:client
     

3. Use the Client:
- Use the client ID and client secret to authenticate with your API. For example, you can use the client credentials grant to authenticate a machine-to-machine application.

Example Usage

Here is an example of how to use the client credentials grant to authenticate a machine-to-machine application:

1. Create a Client:

bash
   php artisan passport:client --client
   

2. Get the Client ID and Secret:

php
   $clientSecret = $client->secret;
   $clientId = $client->id;
   

3. Authenticate with the API:

php
   $response = Http::asForm()->post('https://passport-app.test/oauth/token', [
       'grant_type' => 'client_credentials',
       'client_id' => $clientId,
       'client_secret' => $clientSecret,
       'scope' => 'your-scope',
   ]);
   $accessToken = $response->json()['access_token'];
   

Additional Steps

- Add the `HasApiTokens` Trait:
- Add the `HasApiTokens` trait to your `User` model to provide helper methods for inspecting the authenticated user's token and scopes:

php
    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Passport\HasApiTokens;

    class User extends Authenticatable
    {
        use HasApiTokens, HasFactory, Notifiable;
    }
    

- Configure the `api` Guard:
- In your `config/auth.php` file, set the `driver` option for the `api` guard to `passport` to use Passport's `TokenGuard` for API authentication:

php
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],
    

By following these steps, you can generate a client ID and client secret in Laravel Passport and use them to authenticate machine-to-machine applications with your API[1][2][3][4][5].

Citations:
[1] https://laravel.com/docs/11.x/passport
[2] https://gist.github.com/valterlorran/35e167742b95fcc34ce609b299f53089
[3] https://laracasts.com/discuss/channels/laravel/laravel-passport-client-id-and-secret-id
[4] https://laravel-docs.readthedocs.io/en/stable/passport/
[5] https://www.youtube.com/watch?v=7nQilkap5Us