Laravel Auth :: guard仅在默认情况下有效

时间:2019-02-12 09:04:48

标签: php laravel authentication laravel-5 guard

我第一次在这里问过任何内容,但不是最有经验的人,所以请保持友好!

我在数据库中设置了两个表-用户和代理。

我尝试了多种方式登录两个用户,理想情况下希望他们能够从同一表单登录并检查两个防护以查看凭据是否有效,但是出于某些原因尝试对守卫进行操作,它将仅对我在auth.php文件中默认设置为该表的表起作用。

我正在尝试的警卫:

Auth::guard('web')->attempt($credentials)

Auth::guard('agent')->attempt($credentials)

如果我将auth.php文件中的默认防护设置为网络防护,则它可以正常工作并从“用户”表中登录匹配的凭据,但是当尝试代理凭据时,它会显示在“网络”标签中:

登录-

  

找到POST 302

代理-

  

找到302个

登录-

  

获取200 OK

因此,这几乎就像它确认凭据正确无误并重新定向,但随后又不使用选定的防护措施继续进行吗?

LoginController - DoLogin

Auth.php configeration

1 个答案:

答案 0 :(得分:0)

在您的config/auth.php副本下面的代码中:

<?php
return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'agent' => [
            'driver' => 'session',
            'provider' => 'agents',
        ]

    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'agents' => [
            'driver' => 'eloquent',
            'model' => App\Agent::class,
        ]
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'agents' => [
            'provider' => 'agents',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
];

在您的登录控制器中:

 public function login(Request $request)
    {
      $this->validate($request, [
        'email'   => 'required|email',
        'password' => 'required|min:6'
      ]);

      if (Auth::guard('agent')->attempt(['email' => $request->email, 'password' => $request->password])) {
        return redirect('/dashboard/agent');
      }

      if(Auth::guard('web')->attempt(['email' => $request->email, 'password' => $request->password])){
         return redirect('/dashboard/user');
      }

        return redirect()->back()->withInput($request->input());


    }