Happy Laracon Week! All accounts are 60% off this week.

Shahrukh4's avatar

Need to let users login with multiple credentials same as login with other account functionality in Gmail services

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.

0 likes
4 replies
jacklin456's avatar

Taylor's put up a new Laracast video about using the new Broadcasting tool in Laravel 5. By the end of the book, you will have covered important topics,HTML5 and Javascript: file upload with progress bar, client-side image resizing and multiple runtimes. There's no shortage of content at Laracasts. The Laracasts snippet, each episode, offers a single thought on some aspect of web development. https://customerfeedbacks.info/

Shahrukh4's avatar

@neven actually you didn't get that right. I was talking about loggin in users multiple time and still maintaining session for all of them. Like if you login in Gmail account and you want to add/login with any other account. you can access both accounts in same browser. I want that kind of functionality.

Thanks btw.

Shahrukh4's avatar
Shahrukh4
OP
Best Answer
Level 4

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 in url 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 your cookie 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.

Please or to participate in this conversation.