Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable auto login on register in Laravel 5?

Tags:

php

laravel-5

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.

like image 651
Jagadesha NH Avatar asked Jul 17 '15 14:07

Jagadesha NH


2 Answers

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());
}
like image 175
Marco Pallante Avatar answered Nov 12 '22 18:11

Marco Pallante


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

like image 1
ppgr Avatar answered Nov 12 '22 20:11

ppgr