用户在数据库中有一个名为verified的字段,它是一个布尔值。它默认设置为0,然后设置为' 1'当用户在注册时通过电子邮件发送给他们的验证链接时。
我需要在尝试登录时拒绝将此布尔值设置为0的用户,并向屏幕闪烁一条消息,说明他们被拒绝的原因。验证设置为' 1'的用户将照常登录。
我写过这个经过验证的'在AuthController.php中的方法,这个方法被' handleUserWasAuthenticated' AuthenticatesUsers.php中的方法
public function authenticated($request, $user)
{
if($user->verified==false)
{
flash('you are not verified');
return redirect('/login');
}
return redirect('/');
}
这是' handleUserWasAuthenticated'利用它的方法。
protected function handleUserWasAuthenticated(Request $request, $throttles)
{
if ($throttles) {
$this->clearLoginAttempts($request);
}
if (method_exists($this, 'authenticated')) {
return $this->authenticated($request, Auth::guard($this->getGuard())->user());
}
return redirect()->intended($this->redirectPath());
}
目前无效。已验证的用户正在登录并发送到主页。但是,未经验证的用户也正在登录,但返回到其他页面。
我忘了提及,我没有写上面的第二种方法,它的生成方式与“AuthenticatesUser.php”中的其他方法类似。当我使用make:auth命令时。
答案 0 :(得分:0)
试试这个
if (Auth::user()->verified == 1)
{
return Redirect::to('/dashboard');
}
答案 1 :(得分:0)
用户登录到应用程序后会触发authenticated
方法,因此您的问题是您尝试将登录用户发送到登录页面。如果未经验证的用户试图访问受保护的页面,则会让他看到它,因为他已登录。
如果用户未经过验证,可能的解决方案是退出:
if($user->verified==false) {
\Auth::logout();
flash('you are not verified');
return redirect('/login');
}