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!

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month

Written by

You might also like