0

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',
        ],
]
6
  • Could you please show your route declaration and the usage of the middlewares? Commented Jul 7, 2018 at 11:43
  • @AdreAstrian I am updated my answer please check Commented Jul 7, 2018 at 11:52
  • How about using different controllers and/or methods for the two middlewares? Commented Jul 7, 2018 at 12:00
  • @OluwatobiSamuelOmisakin No I want to use same controller. is it possible? My route will be different but controller will be same Commented Jul 7, 2018 at 12:10
  • @zayedhassan, your route declaration is exactly the same in both cases. So If I take the order of the code shown here the auth:admin-api declaration will override the auth:api declaration! Using a different middleware for a api/admin/shop/store route won't give you two paths, right? Commented Jul 7, 2018 at 12:29

5 Answers 5

1

You should use Auth::guard('role')

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

1 Comment

so every time I have to use if, else. Does not have any other option?
0

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.

Comments

0

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(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.