Posted inTechnology / Laravel / php / Programming

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:

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

Admin Migration:

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


app/Admin.php


Phase 3: Auth Config Setting

config/auth.php

 [
        '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', 'DashboardController@index')->name('dashboard');
 
Route::get('admin-login', 'Auth\mainAdministartorController@showLoginForm');
Route::post('admin-login', ['as'=>'admin-login','uses'=>'Auth\mainAdministartorController@login']);

Phase 6: Make a Laravel Controller

app/Http/Controller/Auth/LoginController.php

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

app/Http/Controller/Auth/mainAdministartorController.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' => 'Email or password are wrong.']);
    }
}

Phase 7: Create Blade Files

resources/views/auth/adminLogin.blade.php

@extends('layouts.app')
 
@section('content')
Admin {{ __('Login') }}
@csrf
@if ($errors->has('email')) {{ $errors->first('email') }} @endif
@if ($errors->has('password')) {{ $errors->first('password') }} @endif
@endsection
Our customers made these Tricks for improving - Pakainfo.com System.
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 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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype