How to disable auto login after registration of a new user in laravel?

Asked by Anonymous on 09 Apr 2019 5092 Views

Jiwan on 09 Apr 2019


Disabling auto login after registration of a new user is very important in Laravel if you are securing the auth routes using middleware which means you can't register a user without logging in.

If you don't disable auto login function, you are going to get logged in as a new user every time you register a new user which is annoying.

So, have a look on how to disable auto login after registration of a new user in Laravel.

Your RegisterController method uses RegisterUsers trait which is added right above the method as shown below.

use Illuminate\Foundation\Auth\RegistersUsers;

Open this file which is located inside vendor\laravel\framework\src\Illuminate\Foundation\Auth directory.

Search for the method register which looks as shown below.

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
      ?: redirect($this->redirectPath());
}

$this->guard()->login($user) is the piece of code that makes the new user log in after registration.

Either remove this piece of code or modify it as per your requirement to prevent the new user log in after registration.


You must be logged in to answer a question. or