Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth driver [passport] for guard [api] is not defined. after updating #1118

Closed
Garanaw opened this issue Nov 16, 2019 · 11 comments
Closed

Auth driver [passport] for guard [api] is not defined. after updating #1118

Garanaw opened this issue Nov 16, 2019 · 11 comments

Comments

@Garanaw
Copy link

Garanaw commented Nov 16, 2019

  • Passport Version: 8.0.0
  • Laravel Version: 6.5.1
  • PHP Version: 7.2.19
  • Database Driver & Version: MySQL 5.7
  • System: Windows 7
  • Server: Laragon

Description:

After updating Laravel from 5.8 to 6.5 and Passport from 7.2 to 8.0, this message appears, no configuration has changed, nor anything that might affect the functioning of Passport.

Steps To Reproduce:

  • Install Laravel V5.6
  • Install Passport (compatible version)
  • Use custom migrations (no changes though, just to keep them in the same directory)
  • Create a working API with Axios in the frontend
  • Update Laravel to 5.8
  • Update dependencies
  • Keep using the API
  • Update Laravel to ^6.0
  • Update dependencies
  • Update Passport to 8.0 (Following the UPGRADE.MD guide)
  • By this moment, the error should appear even though nothing changed in the API calls

Config file

/**
 * This file has not changed since the first installed version
 */
return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport', // This should be the erroring line, in Illuminate\Auth\AuthManager it checks for the existence of a method 'create'.ucfirst($config['driver']).'Driver' which does not exist
            'provider' => 'users',
            'hash' => false,
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
]

Stack Trace

{
    "message": "Auth driver [passport] for guard [api] is not defined.",
    "exception": "InvalidArgumentException",
    "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\AuthManager.php",
    "line": 97,
    "trace": [
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\AuthManager.php",
            "line": 68,
            "function": "resolve",
            "class": "Illuminate\\Auth\\AuthManager",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php",
            "line": 62,
            "function": "guard",
            "class": "Illuminate\\Auth\\AuthManager",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php",
            "line": 41,
            "function": "authenticate",
            "class": "Illuminate\\Auth\\Middleware\\Authenticate",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Auth\\Middleware\\Authenticate",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\ThrottleRequests.php",
            "line": 59,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 105,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
            "line": 682,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
            "line": 657,
            "function": "runRouteWithinStack",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
            "line": 623,
            "function": "runRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
            "line": 612,
            "function": "dispatchToRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 176,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 130,
            "function": "Illuminate\\Foundation\\Http\\{closure}",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\barryvdh\\laravel-debugbar\\src\\Middleware\\InjectDebugbar.php",
            "line": 65,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Barryvdh\\Debugbar\\Middleware\\InjectDebugbar",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\fideloper\\proxy\\src\\TrustProxies.php",
            "line": 57,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Fideloper\\Proxy\\TrustProxies",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php",
            "line": 62,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
            "line": 105,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 151,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
            "line": 116,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "C:\\route\\to\\project\\public\\index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        }
    ]
}

Aditional info

At the moment of updating Passport to 8.0, I had a look to my vendor folder and compared it to the repo and the last commits. I could notice that the exception src\Exceptions\OAuthServerException was missing, so I deleted the folder and reinstalled it using the last version to make sure everything loads fine. Still, this happened after getting the error, so should not be related, but something to let you know I think.

Not sure if the error is due to a service provider not registering the guard properly or if it comes from another piece of code, but as mentioned previously, my auth.php config file has always been the same, and it worked previous to the update

--- EDIT ---

More info

After debugging the project, I realized that the method registerGuard in the PassportServiceProvider class is called, but the closure to register the guard is never executed. The method:

    protected function registerGuard()
    {
        // This closure is registered
        Auth::resolved(function ($auth) {
            // But the content of the closure is never executed
            $auth->extend('passport', function ($app, $name, array $config) {
                return tap($this->makeGuard($config), function ($guard) {
                    $this->app->refresh('request', $guard, 'setRequest');
                });
            });
        });
    }

--- EDIT 2 ---

Workaround

In order to make the closure being executed, I have overriden the original registerGuard method as follows, and it seems to work without issues:

<?php declare(strict_types = 1);

namespace App\Providers;

use Laravel\Passport\PassportServiceProvider as BasePassportServiceProvider;

class PassportServiceProvider extends BasePassportServiceProvider
{
    /**
     * Workaround to fix the closure not being executed ever
     *
     * @return void
     */
    protected function registerGuard()
    {
	$this->app['auth']->extend('passport', function ($app, $name, array $config) {
	    return tap($this->makeGuard($config), function ($guard) {
		$this->app->refresh('request', $guard, 'setRequest');
	    });
	});
    }
}

config/app.php

//Laravel\Passport\PassportServiceProvider::class,
App\Providers\PassportServiceProvider::class, // Overriden
@alwintom
Copy link

Same issue here.

Versions:
Laravel: v6.5.1
Passport: v8.0.0

During debugging I also find that the closure in registerGuard() is not getting executed.
Workaround by @Garanaw works for me too. Thanks

@driesvints
Copy link
Member

This is related to #1003 I believe.

Ping @crynobone

@crynobone
Copy link
Member

Need more information.

All the code are executed on ServiceProvider::register(). Unless for some reason auth was resolved by some service provider during register() instead of boot() then it is possible this could cause an issue.

@crynobone
Copy link
Member

    public static function resolved(Closure $callback)
    {
        $accessor = static::getFacadeAccessor();

        if (!! static::$app->resolved($accessor)) {
            $callback(static::getFacadeRoot());
        }

        static::$app->afterResolving($accessor, function ($service) use ($callback) {
            $callback($service);
        });
    }

A solution is to add static::$app->resolved($abstract) check to cover such edge cases.

@driesvints
Copy link
Member

@crynobone okay. Can you maybe PR that to the framework?

@crynobone
Copy link
Member

@driesvints
Copy link
Member

@crynobone thanks!

@driesvints
Copy link
Member

PR was merged. We can try this out tomorrow when the next framework version is released.

@driesvints
Copy link
Member

@Garanaw can you try again with the latest Laravel release?

@Garanaw
Copy link
Author

Garanaw commented Nov 21, 2019

@driesvints @crynobone
Confirmed: after updating Laravel Framework to v6.5.2 and Laravel Passport to v8.0.1 (although it wasn't required, I decided to also go with the latest release), the AuthManager is extended with the Passport's guard, doesn't throw errors and lets me login with a token. This can be marked as solved.
Thank you

@driesvints
Copy link
Member

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants