【问题标题】:Laravel Auth::guard('employee')->attempt not workingLaravel Auth::guard('employee')->尝试不工作
【发布时间】: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


    【解决方案1】:

    问题是您的密码必须在数据库中散列

    使用功能

    $password=bcrypt($request['password']);
    

    或

    Hash::make($data['password'])
    

    【讨论】:

    【解决方案2】:

    在登录控制器中更改您的代码如下:

    public function adminLogin(Request $request)
    {
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required|min:6'
        ]);  
    
        if (Auth::guard('employee')->attempt(['username' => $request->input('username'), 'password' => $request->input('password')])) {
            return redirect()->intended('/employee/overview');
        }
    
        return back()->withInput($request->only('username'));
    }
    

    【讨论】:

      【解决方案3】:

      attempt 函数,查找哈希密码。它需要输入密码,对其进行哈希处理,然后将其与您在数据库中的密码进行比较。因此,当您在数据库中保存数据时,请确保您这样做:

      $user->password = Hash::make($request->password);
      $user->save();
      

      【讨论】:

        猜你喜欢
        • 2013-05-30
        • 2019-10-01
        • 1970-01-01
        • 2016-09-07
        • 2017-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 2022-01-26
        相关资源
        最近更新 更多