Overriding Login and Registration Functionality in Laravel 5

2015-02-21 Laravel

Laravel 5 comes with some already built-in features like authentication and registration and that makes this framework great. However, sometimes you may want to change the default functionality or behavior of existing elements. As we all know, you shouldn’t touch any of the core scripts (such as files located in the vendor directory, since it can be overwritten during update and you will loose everything that you have changed).

There’s a better way how to do this. In this article I will show you how to change the functionality of the trait that is responsible for handling the registration process. As an example I will pick both authentication and registration method.

Before I will explain what exactly we are going to do, open

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

Take a look at line 21. Authentication controller uses a trait

use AuthenticatesAndRegistersUsers;

Open this trait. It’s 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 login form,
  • postLogin() – responsible for authentication user

Those methods are called every time you will access specific route (auth/register or auth/login). Let me show you. Open terminal and under your Laravel’s root directory type

php artisan route:list

you should see

Laravel 5 - overriding methods - artisan routes preview

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

Every time after successful authentication, user is redirected to a specific URL. If this URL is not set, then the default one will be used. See the method redirectPath.

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

This method is called in postLogin. If we don’t have a home route, we should change it to something different. Open mentioned AuthController.php and create new private property called redirectTo and specify new URL.

protected $redirectTo = '/admin';

Now, after successful authentication, user will be always redirected to your dashboard.

The same as we changed the property, we can change the method. Let’s disable registration globally. We don’t allow anyone to set up an account.

Again in AuthController.php add two methods with the same name as those in trait.

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

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

As you see, now when user will hit a route that should show or process registration, a redirection to the main page will take place.

Your whole controller 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('/');
    }

}

If you know any better method do achieve the same effect, please let me know in a comment below.