@Damen You don’t need to create a new guard or modify Laravel’s authentication at all. All you really need to do is add a “where” clause to your login query.
Using Laravel’s LoginController, you can do this by overriding the credentials() method. This specifies the array that should be passed to Auth::attempt(). You can modify it to include the current company by doing something like:
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password') + [
'tenant_id' => app(Tenant::class)->getKey(),
];
}
This means you can also use the in-built login routes:
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
How you set the tenant in the container is up to you. If you’re placing tenants’ routes in a group, then you could do something like:
Route::group(['domain' => '{tenant}.example.com'], function ($tenant) {
// Set tenant in container
app(Tenant::model)->instance($tenant);
// Authentication
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
});