Adding Authentication to Laravel 5.1

Laravel 5 came out with a nice Auth scaffolding, which included all of the routes/controllers/views for registering and authenticating a user. Laravel 5 got away with this to keep the product slim (which I think is a good move).

Screen Shot 2015-08-17 at 1.57.44 PM.png

Laravel does have documentation on building authentication into your Laravel 5.1 app. However, I’ll go through this in a bit more detail below…

Install Laravel #

First, make sure you have a fresh install of Laravel. Here is my tutorial on Installing Laravel 5.1 on OSX with MAMP.

Twitter Bootstrap #

After downloading bootstrap add the bootstrap.css file into the public/css directory. (you may have to create the css directory.

Also copy over bootstrap’s fonts directory into your app’s public directory.

Authentication Routes #

Add the following routes to the app/Http/routes.php file.

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

// Password reset link request routes...
Route::get('password/email', 'Auth\PasswordController@getEmail');
Route::post('password/email', 'Auth\PasswordController@postEmail');

// Password reset routes...
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
Route::post('password/reset', 'Auth\PasswordController@postReset');

Authentication Views #

First let’s create a blade template to use for all of our other views. We can do that by creating a resources/views/auth/app.blade.php file. And copy/paste the code shown here:

Create a new resources/views/auth directory. Within that directory, create the following files.

For the “forgot password” email, create a resources/views/emails directory, and place the following file into it.

Create Database & Tables #

To make it so that we can actually register a new user and login, we’ll have to create the proper database tables. Fortunately, this is already available through migrations.

First, create a new database table, and define it’s connection in the .env file.


The trigger the migration with the following command:

php artisan migrate

Since I’m using MAMP, I got this error when trying to migrate.


SQLSTATE[HY000] [2002] No such file or directory

Solution was to add the unix_socket key with a value of the path that the mysql.sock resides in MAMP.

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',

Setup SMTP Server #

Laravel 5.1 defaults to First time I gave this a try, and it’s actually quite easy! First step is to setup account.

Update .env file with SMTP settings (provided after signing up)


Update from address in config/mail.php file.

'from' => ['address' => '', 'name' => 'test'],

Create Dashboard #

Add dashboard routes

Route::get('dashboard', 'Dash\DashboardController@home');

Add dashboard controllers to app/Http/Controllers/Dash/DashboardController.php


namespace App\Http\Dash\Controllers;

use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;

class DashboardController extends Controller
    public function home(Request $request)
        return view('dashboard/home');

Note the use of use App\Http\Controllers\Controller;. This is important since were using a different namespace for our dashboard.

And the view at resources/views/dashboard/home.blade.php:


<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    You are logged in!

Update login redirects:


return redirect('/dashboard');

Add to Auth/PasswordController.php and Auth/AuthController.php files.

protected $redirectTo = '/dashboard';

Authenticate Dashboard #

To restrict access to the dashboard to only those that are logged in, we can ddd the following to the Dashboard controller

public function __construct() 

Now read this

Difference between what is and what you want.

I get very anxious when I have two people that want something done at the same time. I think many people would laugh at that - those that have dozens of people that need something done now. But it only takes two for me. I think a part of... Continue →