I'm facing an issue with implementing login functionality in Laravel, where I need to log in a user using different guards based on a specific action. Specifically, I'm trying to implement the loginAsClient feature in my ClientController, which allows an administrator to log in as a client from another part of my application.
My problem lies in the fact that even though the user seems to be logged in correctly (as confirmed by the output from info(Auth::user())), the redirection to the intended URL still redirects me to the standard login page instead of the dashboard.
public function loginAsClient(Request $request) {
$id = $request->input('id');
// Get information about the guard and intended URL
$guard = session('guard', 'web'); // Defaults to "web" if not defined
$urlIntended = session('url.intended', 'dashboard'); // Defaults to "dashboard" if not defined
// Set the current guard
Auth::shouldUse($guard);
$user = User::find($id);
if ($user) {
// Log in the user
Auth::guard($guard)->loginUsingId($user->id, true);
// Manually set the intended URL
session(['url.intended' => $urlIntended]);
info(Auth::user());
info('Current URL: ' . url()->current());
info('Intended URL: ' . session('url.intended'));
// Redirect to the intended URL with information about the guard
return redirect()->intended($urlIntended)->with('guard', $guard);
} else {
return redirect()->back()->with('error', 'Client with this ID was not found.');
}
}
Here is also my auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'system_users' => [
'driver' => 'session',
'provider' => 'system_users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'system_users' => [
'driver' => 'eloquent',
'model' => App\Models\SystemUser::class,
],
I should note that in my Laravel application, I'm using two different tables for user authentication. For administrators, the authentication is handled by the system_users table, and for regular users, it's the users table. The guard is dynamically switched based on the user type.
In my project, I have a dual-administration setup, distinguishing between the two based on the domain. This is achieved in my web.php routes file, where I use different domains to route requests to the respective administration controllers.
web.php whole file:
$domainName = ".com";
if (env('APP_ENV') == 'local') {
$domainName = ".local";
}
$allowedDomains = ['client' => 'client.domain'.$domainName, 'admin' => 'admin.domain'.$domainName];
Route::group(['domain' => $allowedDomains["client"]], function () {
Route::get('/login', [LoginController::class, 'show'])->middleware('guest')->name('login');
Route::post('/login', [LoginController::class, 'login'])->middleware('guest')->name('login.perform');
});
Route::group(['domain' => $allowedDomains["admin"]], function () {
Route::get('/login', [LoginController::class, 'show'])->middleware('guest')->name('loginsystem');
Route::post('/login', [LoginController::class, 'loginSystem'])->middleware('guest')->name('loginsystem.perform');
});
Route::group(['middleware' => 'auth'], function () use ($allowedDomains) {
Route::group(['domain' => $allowedDomains["client"]], function () {
//Dashboard
Route::get('/dashboard', [DashboardController::class, 'index'])->name('home')->middleware('auth');
// ...
// ...
});
});
Route::group(['middleware' => 'auth:system_users'], function () use ($allowedDomains) {
Route::group(['domain' => $allowedDomains["admin"]], function () {
// Dashboard
Route::get('/dashboard', [CooperationController::class, 'index'])->name('admin.dashboard');
Route::post('/client/login-to-client',[ClientController::class, 'loginAsClient'])->name('login-to-client');
// ...
// ...
});
});