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

SyedHabib's avatar

viaRequest method is not working in Lumen Passport

I am implementing Passport in Lumen to authenticate users and APIs using Lumen 10.0 and dusterio/lumen-passport. I followed the articles on the internet to implement it but I stuck at authentication in AuthServiceProvider. In the boot method viaRequest() is not executing. I have enabled $app->configure('auth') and $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, ]) in bootstrap/app.php.

Here is my Providers/AuthServiceProvider.php

<?php

namespace App\Providers;

use App\Models\User;
use App\Models\Admin;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Dusterio\LumenPassport\LumenPassport;

class AuthServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    public function boot()
    {
        LumenPassport::routes($this->app);
       
        var_dump('outside');    //working
        $this->app['auth']->viaRequest('api', function ($request) {
            var_dump('inside'); // not working
            if ($request->header('Authorization')) {
                $key = explode(' ', $request->header('Authorization'))[1];
                return User::where('api_token', $key)->first();
            }
        });
    }
}

My config/auth.php

<?php 

return [
    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

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

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => \App\Models\User::class
        ],
    ]
];

Here is the route

$router->group(['prefix' => 'user', 'middleware' => 'auth:api'], function ($router) {
        $router->get('profile', 'UserController@profile');
        $router->get('logout', 'UserController@logout');
    });

I have tried everything by changing api driver value in viaRequest() method to passport driver then it executes the function but then my logout route does not work to get the access token. I don't know why the viaRequest() is not working with api driver value.

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like the issue is with the middleware registration. Instead of registering the middleware in the bootstrap/app.php file, try registering it in the app/Http/Kernel.php file. Here's how you can do it:

  1. Open the app/Http/Kernel.php file and add the following code to the $routeMiddleware array:
'auth:api' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
  1. Remove the auth middleware registration from the bootstrap/app.php file.

  2. In the AuthServiceProvider class, change the viaRequest method to use the passport driver instead of the api driver:

$this->app['auth']->viaRequest('passport', function ($request) {
    if ($request->header('Authorization')) {
        $key = explode(' ', $request->header('Authorization'))[1];
        return User::where('api_token', $key)->first();
    }
});
  1. Finally, clear the cache by running the following command:
php artisan cache:clear

This should fix the issue and allow you to authenticate using Passport in Lumen.

SyedHabib's avatar

@LaryAI If I change driver to passport then I got following error when logging out user Call to a member function revoke() on null (500 Internal Server Error) here is my logout controller function

public function logout(Request $request) {
        $token = $request->user()->token();
        $token->revoke(); 
        return response()->json(['message' => 'You have been successfully logged out!'], 200);
    }

I am getting Null in accessToken relationship and revoke() method is not working when changing driver in viaRequest().

Please or to participate in this conversation.