如何在Laravel 5中禁用注册时的自动登录? [英] How to disable auto login on register in Laravel 5?
问题描述
我是Laravel的新手,但是爱上了该框架,因此决定将其用于我的项目.
I am new to Laravel but fell in love with the framework and decided to use it for my project.
我有一个字段active
,默认情况下将其设置为0
.在Attempt()
方法中,我设置了$credentials['active'] = 1
.当我注销并再次登录时,可以正常工作.
I have a field active
and by default I've set it to 0
. In the Attempt()
method, I've set $credentials['active'] = 1
. When I logout and login again, this works fine.
但是,当我注册用户时,它会自动将用户登录,而无需检查活动字段.
But when I register a user, it automatically logs the user in without checking active field.
推荐答案
我假设您正在控制器中使用AuthenticatesAndRegistersUsers
特性.
I assume you are using the AuthenticatesAndRegistersUsers
trait in your controller.
该特性中的postRegister()
方法进行注册,该特性在创建新用户后调用login()
方法.
The registration is carried by the postRegister()
method in that trait, which calls the login()
method after creating a new user.
仅当active
字段为true
时,才可以在控制器中覆盖此方法并调用login()
方法.因此,您的postRegister()
方法将类似于:
You can override this method in your controller and call the login()
method only when the active
field is true
. So, your postRegister()
method will be something like:
public function postRegister(Request $request)
{
$validator = $this->registrar->validator($request->all());
if ($validator->fails())
{
$this->throwValidationException(
$request, $validator
);
}
$user = $this->registrar->create($request->all());
if ($request->get('active')) {
$this->auth->login($user);
}
return redirect($this->redirectPath());
}
这篇关于如何在Laravel 5中禁用注册时的自动登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!