18

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Shashika
  • 1,606
  • 6
  • 28
  • 47

4 Answers4

40

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.

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
  • @ivanka what is with registered method at the bottom of register method i'm curious coz it uses ternary operator i wonder what's with it and can i change that with just redirect()->back()->with('info', 'something'); – white-comet Apr 07 '17 at 07:13
  • 4
    In the RegisterController also include `use Illuminate\Http\Request;` and `use 'Illuminate\Auth\Events\Registered;` – shery089 Mar 24 '19 at 18:48
  • Sorry but where the hell can I find the default "register" method, that laravel uses without any self-made modifications? Because I've been looking around and I see nothing about it, not even in the documentation. – ADHDisDEV Jul 27 '23 at 13:05
  • @ADHDisDEV Which version of Laravel are you using? More recent versions have separated the auth into two packages: *Laravel Breeze* and *Laravel Jetstream*. If you are using Breeze, it publishes a new controller in your app and you can [remove this line](https://github.com/laravel/breeze/blob/1.x/stubs/default/app/Http/Controllers/Auth/RegisteredUserController.php#L47). – Ivanka Todorova Jul 28 '23 at 09:11
2

I've added Auth::logout(); next to the guard in the new versions.

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

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

   \Auth::logout();

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

If you use default register route you can do it like this...

in RegistersUsers.php file

comment this line in register function

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

I hope this helps you!!

Akbar Soft
  • 1,028
  • 10
  • 19
  • 9
    This answer suggests editing files within the `vendor/` directory, **which should never be done**! Instead you should define a `register()` method in your `AuthController` (or the equivalent) and change accordingly. Refer to the accepted answer. – Ivanka Todorova Feb 12 '19 at 17:07
  • Yeah. As Ivanka Todorova said, it's not advisable to edit the framework's core files. – Wolverine May 15 '19 at 21:38
-4

You can change the $redirectTo url in the RegisterController to your url. Or You can override registered method of the RegistersUsers trait in RegisterController.

Pankit Gami
  • 2,523
  • 11
  • 19