PHP Multiple Authentication using Laravel 5.7 Middleware

Today, We want to share with you PHP Multiple Authentication using Laravel 5.7 Middleware.In this post we will show you multiple authentication in laravel 5.7 natively (admins + users), hear for Laravel 5.7 – Multiple Authentication Example we will give you demo and example for implement.In this post, we will learn about How to setup multiple authentication in Laravel 5.7 with an example.

PHP Multiple Authentication using Laravel 5.7 Middleware

There are the Following The simple About PHP Multiple Authentication using Laravel 5.7 Middleware Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel 5.7 multiple authentication, so the laravel 5.7 auth with admin and user for this example is following below.

Phase 1: Create Laravel Migration for members and superadmins

Member Migration:

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateMembersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('members', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('members');
    }
}

SuperAdmin Migration:

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateAdminsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('superadmins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('superadmins');
    }
}

Phase 2: Create Member and SuperAdmin Model

app/Member.php

<?php
 
namespace App;
 
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Member as Authenticatable;
 
class Member extends Authenticatable
{
    use Notifiable;
 
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
 
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

app/SuperAdmin.php

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Member as Authenticatable;
class SuperAdmin extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     * PHP Multiple Authentication using Laravel 5.7 Middleware
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Phase 3: Auth Config Setting

config/auth.php

<?php
 
return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'members',
    ],
 
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'members',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'members',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'superadmins',
        ],
    ],
 
    'providers' => [
        'members' => [
            'driver' => 'eloquent',
            'model' => App\Member::class,
        ],
        'superadmins' => [
            'driver' => 'eloquent',
            'model' => App\SuperAdmin::class,
        ]
    ],
 
    'passwords' => [
        'members' => [
            'provider' => 'members',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'superadmins' => [
            'provider' => 'superadmins',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
 
];

Phase 4: Define Laravel Default Auth

php artisan make:auth

Phase 5: Define Laravel Route

routes/web.php

Auth::routes();

Route::get('/dashboard', '[email protected]')->name('dashboard');

Route::get('super-admin-signin', 'Auth\[email protected]');

Route::post('super-admin-signin', ['as'=>'super-admin-signin','uses'=>'Auth\[email protected]']);

Phase 6: Create Laravel Controller

app/Http/Controller/Auth/SigninController.php

<?php
 
namespace App\Http\Controllers\Auth;
 
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesMembers;
 
class SigninController extends Controller
{

    use AuthenticatesMembers;
 
    /**
     * Where to redirect members after login.
     * PHP Multiple Authentication using Laravel 5.7 Middleware	
     * @var string
     */
    protected $redirectTo = '/dashboard';
 
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

app/Http/Controller/Auth/SuperadminsigninController.php

<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesMembers;
use Illuminate\Http\Request;
class SuperadminsigninController extends Controller
{
    use AuthenticatesMembers;
    protected $guard = 'admin';
    /**
     * Where to redirect Laravel members after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';
    /**
     * Create a new Laravel controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function showLoginForm()
    {
        return view('auth.adminLogin');
    }
    public function login(Request $request)
    {
        if (auth()->guard('admin')->attempt(['email' => $request->email, 'password' => $request->password])) {
            dd(auth()->guard('admin')->user());
        }
        return back()->withErrors(['email' => 'Your Email or password are wrong.']);
    }
}

Phase 7: Create Blade Files

resources/views/auth/adminLogin.blade.php

@extends('layouts.app')
 
@section('content')
<div class="pakainfo container">
    <div class="row pakainfo justify-content-center">
        <div class="col-md-8 pakainfo">
            <div class="card pakainfo">
			<a href="https://pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a>
                <div class="gst card-header">SuperAdmin {{ __('Login') }}</div>
 
                <div class="card-body pakainfo">
                    <form method="POST" action="{{ route('super-admin-signin') }}">
                        @csrf 
 
                        <div class="gst form-group row">
                            <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
 
                            <div class="gst col-md-6">
                                <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>
 
                                @if ($errors->has('email'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
 
                        <div class="gst form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
 
                            <div class="gst col-md-6">
                                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>
 
                                @if ($errors->has('password'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
 
                        <div class="gst form-group row">
                            <div class="col-md-6 offset-md-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> {{ __('Remember Me') }}
                                    </label>
                                </div>
                            </div>
                        </div>
 
                        <div class="gst form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-success">
                                    {{ __('Login') }}
                                </button>
                                <a class="gst btn btn-link" href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<a href="https://pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a>
@endsection

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  PHP Send Email using SMTP Authentication

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Multiple Authentication using Laravel 5.7 Middleware.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Recommended For You.

Laravel Check and uncheck all checkbox using jquery
Today, We want to share with you Laravel Check and uncheck all checkbox using jquery.In this post we will show

Add a Comment

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