Disable Auto-Login in Laravel after Registration

Laravel is the most used framework of all times by the PHP community and offers a large number of features and set up out of the box. One of the most common features any website uses is the Authentication system for users.

While for many websites or web apps authenticating or logging in the user right after the registration would be the ideal way but for few, it is not the case. Few websites expect the users to manually login into the application after successful registration or disable auto-login in laravel after registration.

Getting Started

Laravel framework has been beautifully coded and can be customized to match our needs by editing just a few code snippets and we’ll be benefitted from the same easiness of Laravel.

Let’s start off by scaffolding the Authentication system into our fresh application through the predefined commands offered by Laravel Artisan. Although, You can skip the scaffolding if you already have the website or web application setup. It is advised to use the Laravel Authentication scaffolding in the fresh installation of Laravel app only.

Scaffold Authentication in Laravel 5.X

In an earlier version of Laravel i.e. Laravel 5.X, scaffolding authentication does not require to install any packages and can be achieved by entering the php artisan make:auth command right into the console. Once done, Laravel will automatically register the authentication routes and create the view files for you in the resources/views/auth directory. For a better understanding of scaffolding in Laravel 5.X check out the documentation.

Scaffold Authentication in Laravel 6.X

While in the latest version of Laravel, the developers have extracted the Authentication scaffolding into its own package. We’re required to install a package into the application by entering the composer require laravel/ui --dev and php artisan ui vue --auth commands into the console or you can also pass the parameter directly while creating the fresh application laravel new blog --auth and Laravel will take care of the rest for you by registering the required authentication routes and creating the view files for your application in the resources/views/auth directory. For a better understanding of scaffolding in Laravel 6.X check out the documentation.

Disable Auto-Login in Laravel

Laravel makes use of PHP Traits to extend the functionality of the classes by avoiding inheritance and for grouping similar methods into one file. RegistersUsers trait is used inside the RegisterController file located at app/Http/Controllers/Auth directory and it is one of the examples of many traits used inside the Laravel framework.

The RegistersUsers trait makes use of register method to handle the registration of the user and logging in the user at the same time. The API of the register method is as follows.

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

After taking a quick look in the signature of the register method we found out, To match our needs, i.e. Disabling the Auto-login after the registration we simply need to override this method. To override this method we’ll simply open the RegisterController file located at app/Http/Controllers/Auth directory and create a new method with the same name. The Auto-login is handled by the line $this->guard()->login($user);. So, we can simply comment out or remove this line completely in our new method to disable the Auto-login.

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

Please note: Don’t forget to import the Illuminate\Http\Request and Illuminate\Auth\Events\Registered in the RegisterController file or you might experience the errors.

You can also redirect the user to some specific route such as login by overriding the redirectTo property or show some message on the same Registration page.

Finally! You might want to read our guide on Setting up your Laravel app on Shared-Hosting platform or You might want to spend a few minutes on understanding how you can make routes accessible to both Guest and Authenticated users.

2 Comments

  1. Pingback: How to deploy Laravel App on Shared Hosting. – Shade

  2. Siddharth Gupta June 8, 2020 at 10:30 am

    Please update this post because Laravel 7.x has been changed and this function is not located in RegsterController.php anymore.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.