hakhsin's avatar

How do I assign guard to web or admin in the app with multi auth?

I have 3 subdomain for admins (admin.mydomain.com), users (www.mydomain.com) and storage (storage.mydomain.com). Users and Admins maybe send request to storage. In storage subdomain I want to know source of request (admin or user) for authenticate. In this case I know I have two choice at least. One, define two prefix for requesters. Like this: storage.mydomain.com/admin and storage.mydomain.com (for users).

Route::post('/', 'StorageController@store')->name('store');

Route::prefix('admin')
    ->middleware('assign.guard:admin')
    ->group(
        function () {
            //
        }
    );

Two, in request controller constructor I detect source request. Like this:

/**
 * Create a new StorageController instance.
 *
 * @return void
 */
public function __construct()
{
    if (Request::is($adminUrlPattern)) {
        $this->middleware('assign.guard:admin');
    }
        
    $this->middleware('auth');
}

Which one is better and more common?

0 likes
1 reply
Mahin's avatar

I would go with the second option. In my experience this approach is more versatile than using middleware in the route file.

2 likes

Please or to participate in this conversation.