phplaravellaravel-authentication

Disable auto login after Laravel registration


I need to disable auto login after registering a new user in a Laravel application.

I have found examples for older versions, but since Laravel 5.4 there is no AuthController as it divided to LoginController and RegisterController.


Solution

  • Since your RegisterController uses RegistersUsers trait, all of the trait's methods are available to RegisterController. The method you need to override, in order to prevent users to be logged in after they successfully registered is register(). Here's the initial body of the method:

    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());
    }
    

    The line: $this->guard()->login($user); is where the user gets logged in. You can either remove it or modify it to suit your needs.