Reputation: 55
I'm new to the lumen. I've searched over the internet and found no proper answer so I'm posting this question. I've implemented authentication and it works really well. Now the problem is when I'm trying to implement multiple authentications with auth guard middleware, I've no clue how to implement it.
The thing I've done so far -
uncomment these in bootstrap/app.php
$app->withFacades();
$app->withEloquent();
$app->routeMiddleware(['auth' => App\Http\Middleware\Authenticate::class,]);
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
create a migration, model, and login/register controller for customer and admin.
create config/auth.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
*/
'defaults' => [
'guard' => env('AUTH_GUARD', 'customer'),
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
*/
'guards' => [
'customer' => [
'driver' => 'token',
'provider' => 'customers',
],
'admin' => [
'driver' => 'token',
'provider' => 'admins',
]
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
*/
'providers' => [
'customers' => [
'driver' => 'eloquent',
'model' => App\Model\Customer\Customer::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Model\Admin\Admin::class,
]
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
*/
'passwords' => [
//
],
];
My auth service provider
public function boot()
{
$this->app['auth']->viaRequest('token', function ($request) {
if ($request->input('api_token')) {
return Customer::where('api_token', $request->input('api_token'))->first();
}
});
}
If you notice, it is currently authenticating only for the Customer model. No matter what guard I'm using. Please let me know how to make it dynamic.
** I'm not using JWT or any other driver.
Upvotes: 1
Views: 4071
Reputation: 31
This is probably late, but i ran into a similar issue too. I tried using auth guards too but i could not get it to work, I used route prefixes instead. After reading the docs Again, you may retrieve the authenticated user however you wish. I tried this
public function boot()
{
$this->app['auth']->viaRequest('token', function ($request) {
if ($request->input('api_token')) {
if ($request->is('c/*')) {
// Prefix for Customer endpoints
return Customer::where('api_token', $request->input('api_token'))->first();
} elseif ($request->is('admin/*')) {
// Prefix for Admin endpoints
return Admin::where('api_token', $request->input('api_token'))->first();
}
}
});
}
You can also use Gates/Policies
Upvotes: 3