3

Laravel 5 came out with a nice Auth scaffolding, which included all of the routes/controllers/views for registering and authenticating a users. But I started using Laravel 5.1 recently, and noticed authentication is no longer built in. How can I add it back?

Marty Thomas
  • 857
  • 2
  • 9
  • 18

2 Answers2

7

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

Installing Laravel

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

Add 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.

Add 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');

Add 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: https://github.com/laravel/laravel/blob/5.0/resources/views/app.blade.php

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.

DB_HOST=localhost
DB_DATABASE=name
DB_USERNAME=root
DB_PASSWORD=xxxxxxx

The trigger the migration with the following command:

php artisan migrate

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

[PDOException]                                    

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 mailtrap.io. First time I gave this a try, and it's actually quite easy! First step is to setup mailtrap.io account.

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

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=xxxxxx
MAIL_PASSWORD=xxxxxxx
MAIL_ENCRYPTION=null

Update from address in config/mail.php file.

'from' => ['address' => 'noreply@test.com', 'name' => 'test'],

Create Dashboard

Add dashboard routes

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

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

 <?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:

   @extends('app')

@section('content')
<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!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Update login redirects:

  • Update app/Http/Middleware/RedirectIfAuthenticated

with:

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() 
{
    $this->middleware('auth');
}
Community
  • 1
  • 1
Marty Thomas
  • 857
  • 2
  • 9
  • 18
0

Aside from the docs as pointed by Marty Thomas you can also try to use this package for auth scaffolding.

codegenin
  • 450
  • 4
  • 14
  • Ya.. I gave this package a try as well. I'm just a control freak and like to know exactly whats going on, and keep the controllers/views all organized in the same place. But the package does work quite nicely. – Marty Thomas Sep 10 '15 at 14:33