skip to Main Content

i have a website with separate fornt(vue) and backend (laravel) applications

im using passport for authentication , generating personal access token for users and storing it in front application , and sending request with Bearer authToken to backend from front app for protected routes

i have a personal client to generate these tokens

now im trying to grant another website users to access their information in my website , so i have created a new passport client for 3rd party apps

and ask for authorization from 3rd app using that client

     $query = http_build_query([
        'client_id' => config('mywebsite.auth_client_id'),
        'redirect_uri' => route('thisapp.callback'),
        'response_type' => 'code',
        'scope' => '',
        'state' => Str::uuid()->toString() ,
    ]);
    
    return ['url' => config('mywebsite.mywebsite_url') .'/oauth/authorize?'.$query];

which generates a authorizations url

http://api.mywebsite.com/oauth/authorize?....

the problem is this will generate a backend route , which is protected by auth:web

users need to login in my backend directly which means i need a frontent app for login in my backend app

i tried to call this route via my front app with user token , but apparently this route is protected with web/session guard and dont recognize my front token

i’ve tried to change the guard in config/passport to api so the route works with token/api guard but im getting this error

"message":
"LaravelPassportHttpControllersAuthorizationController::__construct():
Argument #2 ($guard) must be of type
IlluminateContractsAuthStatefulGuard,
LaravelPassportGuardsTokenGuard given",

i tried to overwrite the route with auth:api middleware

Route::get('/oauth/authorize', [LaravelPassportHttpControllersAuthorizationController::class, 'authorize'])->middleware('auth:api');

but still asking for log in when i call the route with my auth token

is there any way to call these routes with api/token logged user from front end instead of web/session in the backend ?

2

Answers


  1. The error you’re encountering indicates a type mismatch in the constructor of the AuthorizationController class in Laravel Passport. Specifically, the second argument ($guard) is expected to be of type IlluminateContractsAuthStatefulGuard, but a LaravelPassportGuardsTokenGuard is being passed instead.

    This typically happens because TokenGuard is used for API authentication, whereas StatefulGuard is used for session-based authentication, which is common in web authentication.

    To resolve this issue, you need to ensure that the correct guard type is being injected. Here’s a step-by-step approach to troubleshoot and fix this issue:

    Step 1: Check the Controller Constructor

    Open the AuthorizationController class and check the constructor to understand what dependencies it requires.

    use IlluminateContractsAuthStatefulGuard;
    
    public function __construct(StatefulGuard $guard, /* other dependencies */) {
        $this->guard = $guard;
        // other initializations
    }
    

    Step 2: Configure the Guard in the Auth Service Provider

    Ensure that the correct guard is being configured and passed to the controller. In most cases, you will define this in the AuthServiceProvider or in the service container bindings.

    Step 3: Define the Correct Guard in the Service Container

    You may need to bind the StatefulGuard to the appropriate guard implementation. This can be done in the AppServiceProvider or a similar service provider.

    use IlluminateSupportServiceProvider;
    use IlluminateContractsAuthStatefulGuard;
    use IlluminateSupportFacadesAuth;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function register()
        {
            $this->app->bind(StatefulGuard::class, function ($app) {
                return Auth::guard('web'); // or the appropriate guard
            });
        }
    
        public function boot()
        {
            //
        }
    }
    

    Step 4: Ensure Guard Configuration in config/auth.php

    Make sure your config/auth.php file has the correct guards configured:

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'passport', // or 'token' if you are using Laravel Passport
            'provider' => 'users',
        ],
    ],
    

    Step 5: Use the Correct Guard in Routes and Middleware

    When defining routes or middleware that use the AuthorizationController, ensure they are using the correct guard. For instance, web routes should use the web guard, while API routes should use the api guard.

    Example of Middleware Usage

    If you are using middleware, ensure it references the correct guard:

    Route::middleware(['auth:web'])->group(function () {
        // Define routes that require the web guard
    });
    

    By following these steps, you should be able to ensure that the correct type of guard is injected into the AuthorizationController, resolving the type mismatch issue.

    If the problem persists, ensure you have the latest version of Laravel Passport and Laravel, as there might be updates or fixes related to this issue.

    Login or Signup to reply.
  2. I think you are searching for Passport JSON APIs

    Passport contains the webpages for login, you can use that to redirect to the common page to login for multiple third party applications

    Also Passport contains predefined JSON APIs to issue and verify tokens of the system, for this you can use your own UI, but the common system for Authentication & Authorisation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search