I am new to Laravel but fell in love with the framework and decided to use it for my project.
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.
I assume you are using the AuthenticatesAndRegistersUsers
trait in your controller.
The registration is carried by the postRegister()
method in that trait, which calls the login()
method after creating a new user.
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());
}
In registersUsers.php replace the line:
Auth::guard($this->getGuard())->login($this->create($request->all()));
With the following:
$this->create($request->all());
This worked for me, I use Laravel 5.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With