我正在研究用户身份验证。当用户提供无效的电子邮件或密码时,我想用一条错误消息将其重定向到login.blade.php
。
// Auth/LoginController:
public function login(Request $request)
{
$user=User::where('email',$request->email)->first();
if(!is_null($user))
{
if($user->status==1)
{
if(Auth::guard('web')->attempt(['email'=>$request->email,'password'=>$request->password],$request->remember))
return redirect()->intended(route('index'));
}
}
else
{
if(Auth::guard('web')->attempt(['email'=>$request->email,'password'=>$request->password],$request->remember))
{
if(Auth::check())
{
Auth::logout();
}
$user->notify(new VerifyRegistration($user));
session()->flash('success','A new confirmation mail has sent to you...Please check and confirm your email');
return redirect('/');
}
else
{
session()->flash('errors','Password is incorrect!!');
return redirect()->route('login');
}
}
}
else
{
session()->flash('errors','Email or Password is incorrect!!');
return redirect('/login');
}
}
我正在使用默认的Laravel login.blade.php
。
答案 0 :(得分:1)
当用户输入错误的密码而不是设置会话闪存消息时,请使用下面给出的代码在其他情况下重定向用户。
return redirect()->route('login')->with('error','Invalid password.');
在登录模板上,您可以显示类似错误
@if (session('success'))
<div class="alert alert-success">
<p>{{ session('success') }}</p>
</div>
@endif
@if (session('warning'))
<div class="alert alert-warning">
<p>{{ session('warning') }}</p>
</div>
@endif
@if (session('error'))
<div class="alert alert-error">
<p>{{ session('error') }}</p>
</div>
@endif