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 articles

Laravel 5 Export To PDF Using Laravel-Dompdf

In this article, we will discuss Laravel 5 Export...

Laraver 5 Image upload to BLOB mysql

In this article, we will discuss Laravel 5 save...

Laravel 5 Zipp invoices files using zipper

Requirements Install chumper/zipper Package composer require chumper/zipper Config define providers and...

Laravel 5 OAuth2 for REST API

In this article, I will show you how to...

Case Studies

Psycholog dziecięcy Olsztyn
Search engine optimization

SEO for Child Psychologist Olsztyn

In this case study, we explore how we improved the online presence of a child psychologist in Olsztyn through an effective SEO strategy. The...
Branding & visual design

Web3 DeFi Platform

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...
Content & copywriting

iFlow Attendance App

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...