Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

m7vm7v's avatar
Level 51

How to test the laravel passport api routes

I am on the latest Laravel with initial passport installation

In my test i have -

        $userEmail = '[email protected]';
        $userPassword = 'my-password';
        $userApiSecretName = 'token';
        $userApiSecretRedirect = 'https://test.com/callback';

        $user = factory(User::class)->create([
            'email'    => $userEmail,
            'password' => $userPassword,
        ]);

        $this->actingAs($user, 'api');

        $client = factory(Client::class)->state('password_client')->create([
            'user_id' => auth()->user()->id,
        ]);

        $response = $this->post('/oauth/token', [
            'form_params' => [ //with or without the 'form_params' part
                'grant_type'    => 'password',
                'client_id'     => $client->id,
                'client_secret' => $client->secret,
                'username'      => $userEmail,
                'password'      => $userPassword,
            ],
        ]);

I receive The authorization grant type is not supported by the authorization server

But works fine when I use it from the axios such as

axios.post('/oauth/token', {
                    grant_type:    'password',
                    client_id:     'the client id',
                    client_secret: 'the secret',
                    username:      '[email protected]',
                    password:      'my-password',
                })

From the test it does not work but from the axios works fine.

Any help will be appreciated. Thanks

0 likes
5 replies
frankincredible's avatar

In your test, try changing this line:

$this->actingAs($user, 'api');

to:

$this->actingAs($user, 'passport');

frankincredible's avatar
Level 14

Actually, looking closer at the documentation, you want to use the Passport specific actingAs:

https://laravel.com/docs/7.x/passport#testing

Passport's actingAs method may be used to specify the currently authenticated user as well as its scopes. The first argument given to the actingAs method is the user instance and the second is an array of scopes that should be granted to the user's token:

use App\User;
use Laravel\Passport\Passport;

public function testServerCreation()
{
    Passport::actingAs(
        factory(User::class)->create(),
        ['create-servers']
    );

    $response = $this->post('/api/create-server');

    $response->assertStatus(201);
}
m7vm7v's avatar
Level 51

Hmm thanks @frankincredible . It returns InvalidArgumentException: Auth guard [passport] is not defined. but it makes sense as i have

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

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

in my auth.php

m7vm7v's avatar
Level 51

Wow, I am 100% sure i have tried that 1:1 and did not authenticate me... Now when i had a second go it looks its working. Thanks for pushing me trying again that @frankincredible.

1 like

Please or to participate in this conversation.