Laravel 5 Custom Authentication Setup

In this article, we will focus on how to customize basic Laravel authentication. Normally in Laravel 5 Authentication is focused on ‘user‘ model. I will show you how to create the ‘admin‘ model and change the authorization to work with it. Let’s get started.

Authentication

First, we need to create routes and views for authentication purpose with artisan command.

php artisan make:auth

This will create all routes, views, and controllers for authentication.

Admin Model

Next step is to create a new model.

php artisan make:model Admin

This command will create our /app/Admin.php file. We can edit it for our purpose, you can add anything you need for our Admin user, but Laraver authentication requires columns in Admin table which are: email, password, remeber_token . The full file should look like this:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'login', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Auth Config

Now we need to configure our authentication guard and set the default guard for Laravel project.  All auth config is located in file /config/auth.php

  'defaults' => [
       'guard' => 'admin',
       'passwords' => 'admins',
   ],


'guards' => [

       'admin' => [
           'driver' => 'session',
           'provider' => 'admins',
       ],

       'api-admin' => [
           'driver' => 'token',
           'provider' => 'admins',
       ],
   ],


'providers' => [
        'admins' => [
           'driver' => 'eloquent',
           'model' => App\Admin::class,
       ],


'passwords' => [

        'admins' => [
           'provider' => 'admins',
           'table' => 'password_resets',
           'expire' => 60,
       ],
   ],

Database migration

Create a migration file with the command:

php artisan make:migration create_admins_table

And replace file located in folder: /database/migrations/ with code:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('login');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
    }
}

Now you are ready to migrate the database with the command:

php artisan migrate

That’s all folks. Now we are using Admins for authentication in Laravel project. I hope this will help you to resolve a few auth problem in your projects. Enjoin and leave a comment for any queries or doubts.

, ,

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu