问题
I want to let my users to login with different credentials
in the same browser window, which is using the single users
table. If tables were different then I will surely do that with guards
, but the problem is I have to manage the user logins through single table.
Please help me how to manage multiple sessions in the same browser window, as when I login with other account in a new tab the first one goes logout.
Thanks in advance.
回答1:
What I wanted to do was to maintain multiple session
for a user, so he can log in with his other email-ids inside the same browser window in different tabs.
Here we go, how we can manage that and how Gmail
is managing it.
- At first you have to manage that, the
user
want to login with his other account or switch accounts. So you can show him the login page by appending any notation inurl
that shows he want to switch accounts.
If your original login
URL is http://www.examle.com/login
then for multiple login, you can give him URL like http://www.examle.com/u/1/login
(you can increase the number after u/
part as many times you want to switch accounts)
- Then go to your
config/sessions.php
and edit yourcookie
part as follows
<?php
$user_type = ( ( !empty(request()) && (int)request()->segment(2) ) > 0 ? '_'. request()->segment(2) : '');
return [
//.....rest of array
'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'. $user_type //This user_type generate various session keys for your multiple login according to generated URL
),
];
Then you have to change your all URL's as
dynamic
so that it can execute for both your normal route(without '/u/number/url' part
) and with the/u/number/url
part.Define the following variable at the top of your
web.php
/**
* Setting a variable to check if the user is logging in with first or multiple sessions
*/
$user_login = ( (int)request()->segment(2) > 0 ? 'u/'. request()->segment(2) : '' );
/**
* User attempting to login with other accounts
*/
Route::post($user_login. '/login', 'Auth\LoginController@login');
/**
* Get dashboard for filling the registeration forms
* Your entire app URL will now go like this, whether you can use it with user number or without it. It will go smoothly
*/
Route::get($user_login. '/dashboard', ['as' => 'dashboard', 'uses' => 'FormController@getDashboard']);
/**
* User attempting to login with other accounts
*/
Route::post($user_login. '/logout', 'Auth\LoginController@logout');
- This works great. Thanks everyone for the help.
回答2:
Create a new guard in admin auth with same model.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'front' => [
'driver' => 'session',
'provider' => 'clients',
],
In the controller:
if ($this->guard()->attempt(['email' => $request->email, 'password' => $request->password, 'active' => 1])) {
dd(' i am logged in');
}
}
protected function guard()
{
return auth()->guard('front');
}
来源:https://stackoverflow.com/questions/58023205/need-to-let-users-login-with-multiple-credentials-same-as-login-with-other-accou