如何在控制器内使用Laravel中的自定义防护登录用户



我为"customer"设置了一个自定义保护,该保护有一个运行良好的LoginController。登录控制器如下:

public function login(Request $request)
{
// Validate form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
// Attempt to log the user in
if(Auth::guard('customer')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember))
{
return redirect()->intended(route('customer.dashboard'));
}
// if unsuccessful
return redirect()->back()->withInput($request->only('email','remember'))
}

现在,在我的工作流程中,我在另一条路线上查看客户;我有另一个控制器,在那里我创建了一个新客户。然后我试着让他们登录,因为我有他们所有的详细信息,这似乎不起作用,有人知道我做错了什么吗?

public function registerCustomer($data)
{
$pw = Hash::make($data->pw->main);
$customer = new Customer;
$customer->firstname = $data->customer->name;
$customer->lastname = $data->customer->lastname;
$customer->mobile = $data->customer->mobile;
$customer->email = $data->customer->email;
$customer->password = $pw;
$customer->save();
//I HAVE TRIED THIS
// if(Auth::guard('customer')->attempt(['email' => $data->customer->email, 'password' => $pw]))
// {
//   dd('logged in');
// }
//AND NOW THIS...
$logged =  Auth::guard('customer')->attempt([ 'email' =>$data->customer->email, 'password' => $pw ]);
dd($logged);
}

registerCustomer函数中,$pw是一个加密的密码。您无法将其用于Auth::guard('customer')。您必须将$pw替换为$data->pw->main

$logged =  Auth::guard('customer')->attempt([ 'email' =>$data->customer->email, 'password' => $data->pw->main ]);
dd($logged);

最新更新