Overriding Login and Registration Functionality in Laravel 5

approximately 3 minutes of reading

Laravel 5 comes with already built-in features like Authentication and Registration. However, sometimes you may want to change default functionality or behavior of existing components. Altering the code directly inside the vendor directory is not an option as content within may change anytime we pull or upgrade dependencies. In this article I will show you how to alter registration and login functionality in a save an convenient way.

Background

Let's pick both authentication and registration methods as examples we are going to work with. Before we begin, first open:

/app/Http/Controllers/Auth/AuthController.php

Take a look at line 21. The authentication controller uses a trait:

use AuthenticatesAndRegistersUsers;

Open this trait, it is located in:

/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesAndRegistersUsers.php

Here you will find couple of interesting methods such as:

  • getRegister() – responsible for showing registration form,
  • postRegister() – responsible for processing registration request
  • getLogin() – responsible for showing authentication (login) form
  • postLogin() – responsible for processing authentication user

These methods are called every time you access specific route (auth/register or auth/login). Let me show you - open terminal and in the root of your Laravel project type php artisan route:list.

Of course modifying trait methods directly (under vendor directory) is not a good idea. We are allowed to override them but in AuthController.php and that’s what we’re going to do.

Altering Existing Logic

Every time authentication attempt is successful, user is redirected to a specific URL. If this URL is not set, then the default one is used. See the method redirectPath():

return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';

This method is called in postLogin(). If we do not have a home route (or it's different), we should change it accordingly.

Open AuthController.php and create new private property called $redirectTo and specify a new URL, like:

protected $redirectTo = '/admin';

Now, after successful authentication, user is always going to be redirected to admin dashboard (assuming there is one).

Disabling Laravel Registration Entirely

In the previous example we have modified existing property. Let's do the same with a method and as an example we are going to modify the registration logic by simply disabling it globally. We do not allow anyone to set up an account.

How to do this in the most simple way?

Let's go back to the AuthController.php. Simply define two methods with the same name as the ones in the trait we have mentioned earlier:

public function getRegister()
{
    return redirect('/');
}

public function postRegister()
{
    return redirect('/');
}

Now when user will hit the route that should show registration form (or process its submission), a redirection will take place. This is just an example. If you want, instead of redirecting, you can simply call the abort() method, suggesting requester that the resource does not exist.

The entire AuthController should look like:

<?php 

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller 
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers;
  
    protected $redirectTo = '/admin';

    /**
     * Create a new authentication controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
     * @return void
     */
    public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;

        $this->middleware('guest', ['except' => 'getLogout']);
    }
  
    public function getRegister()
    {
        return redirect('/');
    }

    public function postRegister()
    {
        return redirect('/');
    }
}

I hope you have found this article useful.


Words: 622
Published in: Laravel · PHP