Laravel Auth: How to disable auto-login after registration?

Laravel default Auth system is great, but sometimes there’s a need that user would register but NOT log in automatically. The problem is that Laravel does auto-login by default after registration. Worry no more, there is a simple solution for that!

Let’s start with fresh Laravel 5.2 installation and ‘php artisan make:auth’ command called. Of course this will work in existing projects too!

Next, let’s create a nice success page: (for the purpose of our tutorial I’m keeping it really simple)

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">
                <div class="alert alert-success">
                    Success! You have registered, please check your email for more instructions!
                </div>
            </div>
        </div>
    </div>
@endsection

* File was saved under /resources/views/success.blade.php

Now we add a route, in which user will be met with our success message:

Route::get('/auth/success', [
    'as'   => 'auth.success',
    'uses' => 'Auth\AuthController@success'
]);

And finally – open app/Http/Controllers/Auth/AuthController.php and make these changes:

1. Add new method:

    public function success()
    {
        return view('success');
    }

2. Override ‘register’ method:

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    $this->create($request->all());

    return redirect(route('auth.success')); // Change this route to your needs
}

 

That is it! By overriding AuthController::register() method and changing

Auth::guard($this->getGuard())->login($this->create($request->all()));

to

$this->create($request->all());

in the code – you’ve disabled auto-login function. Now your users will see a nice success page instead of being logged in directly!

Like our articles?
Check out our Laravel online courses!

10 COMMENTS

  1. You disabled not only auto-login, but registration too hahaha.
    You forgot the “$this->create($request->all());” method call.

  2. Thanks for the pointers, very useful. I found in my version of Laravel (5.3.25) I needed to make a few changes to the RegisterController.

    use Illuminate\Http\Request;
    use Illuminate\Auth\Events\Registered;

    /**
    * Handle a registration request for the application.
    *
    * @param \Illuminate\Http\Request $request
    * @return \Illuminate\Http\Response
    */
    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());
    }

    Might be useful for someone 🙂

  3. I think a better solution is to override the `registered` method (which is meant to be overriden) and to write the following:

    “`
    public function registered(Request $request, )
    {
    $this->guard()->logout();
    }
    “`

  4. replace

    public function registered(Request $request, )
    {
    $this->guard()->logout();
    }

    public function registered(Request $request, )
    {
    $this->guard();
    }

    • Temporarily I have to modify the register method of the RegistersUsers trait, and looking for a suggestion also.

  5. hi how could i show a session massage to user after register in login page and i know that i use ->with after redirect but where should i use this

LEAVE A REPLY

Please enter your comment!
Please enter your name here