Php Laravel多重保护身份验证失败
我正在创建一个有多个守卫的Laravel网站我的目标是能够使用会话和使用passport以管理员、员工和用户身份登录一切正常但是,我无法以员工身份登录 这是git回购协议。请检查员工分支机构和数据库播种器。 我将在这里分享查找问题所在的步骤和代码:Php Laravel多重保护身份验证失败,php,laravel,authentication,laravel-7,laravel-guard,Php,Laravel,Authentication,Laravel 7,Laravel Guard,我正在创建一个有多个守卫的Laravel网站我的目标是能够使用会话和使用passport以管理员、员工和用户身份登录一切正常但是,我无法以员工身份登录 这是git回购协议。请检查员工分支机构和数据库播种器。 我将在这里分享查找问题所在的步骤和代码: if ($res = $this->guard()->attempt($credentials, $remember_token)) { return redirect()->intended('/employee/home
if ($res = $this->guard()->attempt($credentials, $remember_token)) {
return redirect()->intended('/employee/home');
}
if ($res = $this->guard()->attempt($credentials, $remember_token)) {
return redirect()->intended('/employee/home');
}
这里是config/auth.php文件,我有4个卫士web、api、管理员和员工及其提供商和密码代理:
最后是App\Http\Controllers\Auth\Employee\LoginController.php:
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($guard == "admin" && Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::ADMINHOME);
}
if ($guard == "employee" && Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::EMPLOYEEHOME);
}
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
if ($request->is('admin') || $request->is('admin/*')) {
return route('admin.login');
}
if ($request->is('employee') || $request->is('employee/*')) {
return route('employee.login');
}
if (! $request->expectsJson()) {
return route('login');
}
}
}
<?php
namespace App\Http\Controllers\Auth\Employee;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating employees for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::EMPLOYEEHOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest:employee')->except('logout');
}
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard('employee');
}
public function showLoginForm()
{
return view('auth.employee-login');
}
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
*/
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
$credentials = ['email' => $request->email, 'password' => $request->password];
$remember_token = $request->get('remember');
if ($res = $this->guard()->attempt($credentials, $remember_token)) {
return redirect()->intended('/employee/home');
}
return back()->withInput($request->only('email', 'remember'));
}
public function logout()
{
Auth::guard('employee')->logout();
return redirect('/');
}
}
在LoginController中的此if语句中,请执行以下操作:
--dd(Auth::guard('employee')->check();结果为真。
--dd(Auth::guard('employee')->user()它返回:
App\Employee {#379 ▼
#guard: "employee"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#table: "employees"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:7 [▼
"name" => "employee"
"email" => "employee@gmail.com"
"email_verified_at" => "2020-05-05 18:16:25"
"password" => "$2y$10$15zFxGvAA2GVRkcAYFEXc.3WyOtcdlARlOMwIdSEqbU2.95NNWUJG"
"remember_token" => null
"created_at" => "2020-05-05 18:16:25"
"updated_at" => "2020-05-05 18:16:25"
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
我还是找不到问题出在哪里。。任何帮助。谢谢。问题不在LoginController或问题逻辑中。一切都是对的。然而,员工迁移是错误的,表中没有
id
,这就是修复方法。。所以这个问题无关紧要 可能是if($res=$this->guard('employee')->尝试($credentials,$memored_-token)){
或if($Auth::guard('employee')->尝试($credentials,$memored_-token)){
而不是空的guard()
。检查部分。我不认为if($res this->guard($guard('employee')->尝试($memored40
将起作用,因为guard()是LoginController中的一个函数,它返回Auth::guard('employee')
,并且在括号中没有任何要传递给它的参数。但是,如果我忽略guard()函数并尝试if($res=Auth::guard('employee')->尝试($credentials,$memory\u-token))
我仍然会遇到同样的问题和同样的问题。我在问题中附上了git repo。如果有人需要检查,此代码仅在员工分支机构。这里有太多的失败点。将所有中间件从构造方法中移出,并在路由文件中仅设置中间件组。如果不使用xdebug,这将是最简单的。我的建议是安装并开始使用xdebug,这样您就可以知道行变量的确切变化以及为什么会发生代码中断。很高兴知道。:D
if ($res = $this->guard()->attempt($credentials, $remember_token)) {
return redirect()->intended('/employee/home');
}
App\Employee {#379 ▼
#guard: "employee"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#table: "employees"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:7 [▼
"name" => "employee"
"email" => "employee@gmail.com"
"email_verified_at" => "2020-05-05 18:16:25"
"password" => "$2y$10$15zFxGvAA2GVRkcAYFEXc.3WyOtcdlARlOMwIdSEqbU2.95NNWUJG"
"remember_token" => null
"created_at" => "2020-05-05 18:16:25"
"updated_at" => "2020-05-05 18:16:25"
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"