Step-by-step: Laravel 5 authentication

Laravel, Laravel 5, 2015-06-16 23:57:33 UTC

By default, Laravel 5 has included authentication system in the application. It's located in "app/config/auth.php".

But before you begin with Laravel 5 authentication, you must have a Laravel 5 application. If you don't have it one, you can follow very simple steps here.

Lets start creating authentication system;

1. Setting up a database

Open "app/config/database.php"

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', '1234'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],




2. Create users table

Please notice;
First you must take a look at "database/migrations" folder. If you find a file like "2014_10_12_000000_create_users_table.php", you can skip this point.

To create a migration file, run below command;

$ php artisan make:migration create_users_table --create=users
Created Migration: 2015_02_11_090205_create_users_table


Above command create a "2015_02_11_090205_create_users_table.php" file in "app/migrations" folder. Open it and add these line;

$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();



3. Migrate

$ php artisan migrate


If you have phpMyAdmin, you can see tables like below


And users table structure like below;



4. Create a user

Create "app/database/seeds/UserTableSeeder.php" file and add these lines;

class UserTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();

        User::create(array(
            'name'     => 'Codescr',
            'email'    => 'codescr@email.com',
            'password' => Hash::make('codescr')
        ));
    }
}



Open "app/database/seeds/DatabaseSeeder.php" and create code like below;

public function run()
{
    Model::unguard();
    $this->call('UserTableSeeder');
}



then, run this command;

$ php artisan db:seed



If you find an error while executing above command, you need a tricky method like this.



5. Log into application

Open your browser and type "http://your_app/home" and it redirect you to "http://your_app/auth/login". It happen because you are not yet login.




Just type email and password as you create above, signing in and you can see a page like below;






That's it. :)



Share: