Can we use laravel passport with different guards to authenticate APIs for two different types of users. For example we have driver app for driver user and vendor app for vendor user. Both have their different models Driver and Vendor. How can we use different guards to authenticate both types of users using Laravel Passport?
相关问题
- Laravel Option Select - Default Issue
- Laravel 5.1 MethodNotAllowedHttpException on store
- Laravel - Implicit route model binding with soft d
- How to verify laravel passport api token in node /
- laravel : php artisan suddenly stop working
相关文章
- laravel create model from custom stub when using p
- send redirect and setting cookie, using laravel 5
- How to send parameters to queues?
- Bcrypt vs Hash in laravel
- Laravel: What's the advantage of using the ass
- How to make public folder as root in Laravel?
- Input file in laravel 5.2?
- What is the difference between Session::set and Se
Here is an example of auth.php and api.php to start with
config/auth.php
routes/api.php
You have to modify this files:
File: vendor\laravel\passport\src\Bridge\UserRepository.php
Copy/Paste getUserEntityByUserCredentials to make a duplicate of it and name it getEntityByUserCredentials
Then, in the new duplicated function, find the below:
And Replace it with:
File: vendor\league\oauth2-server\src\Grant\PasswordGrant.php
in : validateUser method add after $username and $password :
And this instead of the original line
After doing this you'll be able to pass an extra key/value pair to your access token request, like for example:
I hope this will be helpful for you
I managed to create multiple auths (with laravel/passport) by using a simple middlware.
Step 1: config/auth.php
Add your user classes to providers
Clean the cache via CLI
Step 2: Create middleware
Open the newly created middleware in app/Http/Middleware and update the hand method like below
Step 3: Register your middleware
Add the newly created middleware to $routeMiddleware
and make sure it's at the top of $middlewarePriority
Step 4: Add middleware to route
Step 5: LoginControllers (AdminUserController & BasicUserController)
In summary:
The login controllers use Eloquent models to get the user object and then log the user in through Auth::login($user)
Then for future requests that need authentication, the new middleware will change the api auth guard provider to the correct class.