我是laravel的新手,但我对多认证laravel感到好奇。我想制作一些有两个规则的网站,即客户和卖家。但是,我希望他们登录使用相同的登录表单。我尝试使用php artisan make:auth,但我不明白如何在一个控制器中使用它使用LoginController.php,从我从许多教程看到的,它用不同的登录表单和控制器分开。如客户登录表单和卖家登录表单。是否可以使用一个登录表单和一个登录控制器进行多次身份验证?
谢谢
我想你可以在你的attemptLogin()
中覆盖LoginController
方法,如下所示:
protected function attemptLogin(Request $request)
{
$customerAttempt = Auth::guard('customer')->attempt(
$this->credentials($request), $request->has('remember')
);
if(!$customerAttempt){
return Auth::guard('seller')->attempt(
$this->credentials($request), $request->has('remember')
);
}
return $customerAttempt;
}
public function login(Request $request)
{
// Validate the form data
$validator = $this->validate($request, [
'email' => 'required|email',
'password' => 'required|string'
]);
// Attempt to log the customer in
if (Auth::guard('customer')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(route('Put_your_URL'));
} //attempt to log the seller in
else if (Auth::guard('seller')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(route('Put_your_URL'));
}
// if Auth::attempt fails (wrong credentials) create a new message bag instance.
$errors = new MessageBag(['password' => ['Adresse email et/ou mot de passe incorrect.']]);
// redirect back to the login page, using ->withErrors($errors) you send the error created above
return redirect()->back()->withErrors($errors)->withInput($request->only('email', 'password'));
}