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
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 typeIlluminateContractsAuthStatefulGuard
, but aLaravelPassportGuardsTokenGuard
is being passed instead.This typically happens because
TokenGuard
is used for API authentication, whereasStatefulGuard
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.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 theAppServiceProvider
or a similar service provider.Step 4: Ensure Guard Configuration in
config/auth.php
Make sure your
config/auth.php
file has the correct guards configured: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 theweb
guard, while API routes should use theapi
guard.Example of Middleware Usage
If you are using middleware, ensure it references the correct 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.
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