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:

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:

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


app/SuperAdmin.php


Phase 3: Auth Config Setting

config/auth.php

 [
        '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', 'DashboardController@index')->name('dashboard');

Route::get('super-admin-signin', 'Auth\SuperadminsigninController@showLoginForm');

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

Phase 6: Create Laravel Controller

app/Http/Controller/Auth/SigninController.php

middleware('guest')->except('logout');
    }
}

app/Http/Controller/Auth/SuperadminsigninController.php

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')
Free Download Example - Pakainfo.com
SuperAdmin {{ __('Login') }}
@csrf
@if ($errors->has('email')) {{ $errors->first('email') }} @endif
@if ($errors->has('password')) {{ $errors->first('password') }} @endif
Free Download Example - Pakainfo.com @endsection
Angular 6 CRUD Operations Application Tutorials

Read :

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.

Leave a Comment