【发布时间】:2019-10-13 10:19:33
【问题描述】:
对于我的网站,我希望有 2 个守卫:员工和客户。当我登录或注册时,我总是得到 302。当我登录员工时:
Auth::guard('employee')->attempt(['username' => $request->username, 'password' => $request->password])
我弄错了。我检查了 $request 并且值是正确的。当我在没有警卫的情况下做同样的事情时,我收到一个错误,他无法在用户中找到用户名。所以他使用警卫在右列中搜索,但仍然没有找到它。用户名和密码的值在数据库中。但是,当我播种它时,它没有被散列,因为我只是一个学生,而且它都在测试环境中。这会是个问题吗?我没有在这里发布中间件和所有这些东西,因为当我对上面的行代码执行 dd() 时,我得到“错误”,所以尝试是否存在问题,或者它真的需要中间件这行代码?
我的员工模型如下所示:
class Employee extends Authenticatable {
use Notifiable;
protected $guard = 'employee';
protected $fillable = [
'name', 'username', 'password',
];
protected $hidden = [
'password', 'remember_token',
]; }
auth.php(短版)
'guards' => [
'employee' => [
'driver' => 'session',
'provider' => 'employees',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
]
],
'providers' => [
'employees' => [
'driver' => 'eloquent',
'model' => App\Employee::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
我的登录控制器
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->middleware('guest:employee')->except('logout');
$this->middleware('guest:customer')->except('logout');
}
public function adminLogin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required|min:6'
]);
if (Auth::guard('employee')->attempt(['username' => $request->username, 'password' => $request->password])) {
return redirect()->intended('/employee/overview');
}
return back()->withInput($request->only('username'));
}
我知道这个问题被问了很多但我搜索了很多没有找到解决方案并问我的网络老师但他也没有找到问题。我也许可以使用带有角色的东西,但我发现我不能这样做很愚蠢。我只是想学习正确使用守卫。
提前谢谢你!
【问题讨论】:
标签: php laravel authentication guard