Reputation: 13
I am facing a problem in my application. Let there is two middleware 1)User 2)Admin Is it possible to get which middleware I authenticated in my controller? I am using Laravel 5.4. Here is my route declaration
Route::group(['prefix' => 'user'], function () {
Route::group(['middleware' => ['auth:api']], function () {
Route::post('shop/store', 'ApiShopController@shopStore');
Route::post('shop/update', 'ApiShopController@shopUpdate');
});
});
Route::group(['prefix' => 'admin'], function () {
Route::group(['middleware' => ['auth:admin-api']], function () {
Route::post('shop/store', 'ApiShopController@shopStore');
Route::post('shop/update', 'ApiShopController@shopUpdate');
});
});
Here is my midlleware declaration
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
]
Upvotes: 0
Views: 6843
Reputation: 50491
The config has what is currently the default guard. If you used the auth
middleware than which ever guard did the authenticating is set as the current default:
config('auth.defaults.guard');
Auth::getDefaultDriver();
Upvotes: 0
Reputation: 7334
My understanding about middleware is to help you do a filter on who you allow access a particular route/resources or intercept the request before it hits your resources in your Laravel app and that is why it is placed right during routes declaration or when the constructor is constructed; Check Middleware Introduction
However, for your case I would reconstruct my route declaration to look like this:
Route::group(['middleware' => ['auth:api']], function () {
Route::group(['prefix' => 'user'], function () {
Route::post('shop/store', 'ApiShopController@shopStore');
Route::post('shop/update', 'ApiShopController@shopUpdate');
});
Route::group(['middleware' => ['auth:admin-api']], function () {
Route::group(['prefix' => 'admin'], function () {
Route::post('shop/store', 'ApiShopController@shopStore');
Route::post('shop/update', 'ApiShopController@shopUpdate');
});
});
});
My observation though is that the call to user/shop/store
for example will be hitting shopStore
method in ApiShopController that admin/shop/store
will it.
I advice that you either separate the methods for that each routes are meant to work on or you don't need middleware, since you'll be needing if
conditions to be checking stuffs that your middleware would have done for you or you use different controllers for each middleware group.
PS: Let me know if there is something I missed about your question.
Upvotes: 0
Reputation: 13669
if your application has two guards 'admin' and 'user' in your config/auth.php
...
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'user' => [
'driver' => 'session',
'provider' => 'user',
],
, then you can get current guard
@if(Auth::guard('admin')->check())
Hello {{Auth::guard('admin')->user()->name}}
@elseif(Auth::guard('user')->check())
Hello {{Auth::guard('user')->user()->name}}
@endif
Upvotes: 0
Reputation: 4102
You could include this in your Controller constructor to assign in the a middleware directly, for example like this:
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('home');
}
}
You could run this method in your controller aswell but I did not check it yet:
$middleware = $this->getMiddleware();
That should return the middleware in your controller
Upvotes: 0