Laravel Multiple Authentication Example

Today, We want to share with you Laravel Multiple Authentication Example.In this post we will show you How to use multiple authentication guards in a Laravel app, hear for Creating Multiple Authentication in Laravel 5.5 Using Middleware we will give you demo and example for implement.In this post, we will learn about Multiple Authentication System Laravel 5.4 | 5.5 | 5.6 | 7/6 with an example.Blogs – Pakainfo.com

Laravel Multiple Authentication Example

There are the Following The simple About Laravel Multiple Authentication Example Full Information With Example and Pakainfo.com source code.

As I will cover this Post with live Working example to develop Setting Up Multi-Authentication in Laravel 7/6+, so the some major files and Directory structures for this example is following below.Free Download Example – Pakainfo.com

Phase 1: Create Migration for memebrs and admins

Member Migration:

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateMembersTable extends Migration
{
    public function up()
    {
        Schema::create('memebrs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
 
    public function down()
    {
        Schema::dropIfExists('memebrs');
    }
}

Admin Migration:
<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateAdminsTable extends Migration
{

    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
 
    public function down()
    {
        Schema::dropIfExists('admins');
    }
}

Phase 2: Create Member and Admin Model

app/Member.php

<?php
 
namespace App;
 
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Member as Authenticatable;
 
class Member extends Authenticatable
{
    use Notifiable;
 
    protected $fillable = [
        'name', 'email', 'password',
    ];
 
    protected $hidden = [
        'password', 'remember_token',
    ];
}

app/Admin.php
<?php
 
namespace App;
 
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Member as Authenticatable;
 
class Admin extends Authenticatable
{
    use Notifiable;
 
    protected $fillable = [
        'name', 'email', 'password',
    ];
 
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Phase 3: Auth Config Setting

config/auth.php

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

Phase 4: Make a Laravel Default Auth

php artisan make:auth

Phase 5: Make a Laravel Route

routes/web.php

Auth::routes();
 
Route::get('/dashboard', '[email protected]')->name('dashboard');
 
Route::get('admin-login', 'Auth\[email protected]');
Route::post('admin-login', ['as'=>'admin-login','uses'=>'Auth\[email protected]']);

Phase 6: Make a Laravel Controller

app/Http/Controller/Auth/LoginController.php

<?php
 
namespace App\Http\Controllers\Auth;
 
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesMembers;
 
class LoginController extends Controller
{
    use AuthenticatesMembers;
 
    protected $redirectTo = '/dashboard';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

app/Http/Controller/Auth/mainAdministartorController.php
<?php
 
namespace App\Http\Controllers\Auth;
 
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesMembers;
use Illuminate\Http\Request;
 
class mainAdministartorController extends Controller
{

    use AuthenticatesMembers;
 
    protected $guard = 'admin';
 
    protected $redirectTo = '/dashboard';
 
    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' => '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="pakainfo row justify-content-center">
        <div class="pakainfo col-md-8">
            <div class="pakainfo card">
                <div class="pakainfo card-header">Admin {{ __('Login') }}</div>
 
                <div class="card-body pakainfo">
                    <form method="POST" action="{{ route('admin-login') }}">
                        @csrf 
 
                        <div class="pakainfo form-group row">
                            <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
 
                            <div class="pakainfo 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="pakainfo form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
 
                            <div class="pakainfo 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="pakainfo 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="pakainfo form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Login') }}
                                </button>
                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Our customers made these Tricks for improving – Pakainfo.com System.
Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  what is solid principles? and solid principles c#

Summary

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

I hope you get an idea about Laravel Multiple Authentication Example.
I would like to have feedback on my My Blog 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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Laravel Join Query Example
  2. Read Also:  vue Select Box binding Example
  3. Read Also:  jQuery AJAX POST using PHP Tutorial with Example
  4. Read Also:  laravel multi auth (Authentication) guards Example
  5. Read Also:  laravel 6 auth
  6. Read Also:  how to find duplicate records in sql server?
  7. Read Also:  PHP Multiple Authentication using Laravel 5.7 Middleware
  8. Read Also:  AngularJS Simple calculator web Application
  9. Read Also:  Simple PHP Data Type Conversion Example
  10. Read Also:  Block specific IP Addresses Website Access by Country using PHP