需要让用户使用多个凭据登录,与使用Gmail服务中的其他帐户功能登录一样-Laravel [英] Need to let users login with multiple credentials same as login with other account functionality in Gmail services- Laravel

查看:52
本文介绍了需要让用户使用多个凭据登录,与使用Gmail服务中的其他帐户功能登录一样-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的用户在同一浏览器窗口中使用不同的凭据登录,该窗口使用单个 users 表.如果表不同,那么我肯定会使用 guards 来做到这一点,但是问题是我必须通过单个表来管理用户登录.

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.

谢谢.

推荐答案

我要做的是为用户维护多个会话,这样他就可以使用他的其他电子邮件ID登录相同的浏览器窗口,位于不同的标签中.在这里,我们将介绍如何管理它以及 Gmail 如何对其进行管理.

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.

  • 首先,您必须进行管理,用户要使用其其他帐户登录或切换帐户.因此,您可以通过在 url 中添加任何表示他要切换帐户的符号来向他显示登录页面.
  • 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.

如果您原始的 login URL是 http://www.examle.com/login 那么对于多次登录,您可以给他提供类似 http://www.examle.com/u/1/login 的URL(您可以在 u/部分之后增加数字您想要切换帐户的次数)

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)

  • 然后转到您的 config/sessions.php 并按如下所示编辑 cookie 部分
  • 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
    ),
];

  • 然后,您必须将所有URL都更改为 dynamic ,以便它可以为您的常规路由(没有'/u/number/url'部分)以及/u/number/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.

      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');
      

      • 这很好用.谢谢大家的帮助.
      • 这篇关于需要让用户使用多个凭据登录,与使用Gmail服务中的其他帐户功能登录一样-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆