PHP Multiple Authentication using Laravel 5.7 Middleware – Technology

PHP Multiple Authentication using Laravel 5.7 Middleware – Technology

In this post we will give you information about PHP Multiple Authentication using Laravel 5.7 Middleware – Technology. Hear we will give you detail about PHP Multiple Authentication using Laravel 5.7 Middleware – TechnologyAnd how to use it also give you demo for it if it is necessary.

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 IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
 
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 IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
 
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 IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthMember 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 IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthMember 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

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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' => AppMember::class,
        ],
        'superadmins' => [
            'driver' => 'eloquent',
            'model' => AppSuperAdmin::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 AppHttpControllersAuth;
 
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesMembers;
 
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 AppHttpControllersAuth;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesMembers;
use IlluminateHttpRequest;
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 >
    <div >
        <div >
            <div >
			<a href="https://onlinecode.org/" target="_blank" alt="onlinecode" title="onlinecode">Free Download Example - onlinecode</a>
                <div >SuperAdmin {{ __('Login') }}</div>
 
                <div >
                    <form method="POST" action="{{ route('super-admin-signin') }}">
                        @csrf 
 
                        <div >
                            <label for="email" >{{ __('E-Mail Address') }}</label>
 
                            <div >
                                <input id="email" type="email"  name="email" value="{{ old('email') }}" required autofocus>
 
                                @if ($errors->has('email'))
                                    <span >
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
 
                        <div >
                            <label for="password" >{{ __('Password') }}</label>
 
                            <div >
                                <input id="password" type="password"  name="password" required>
 
                                @if ($errors->has('password'))
                                    <span >
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
 
                        <div >
                            <div >
                                <div >
                                    <label>
                                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> {{ __('Remember Me') }}
                                    </label>
                                </div>
                            </div>
                        </div>
 
                        <div >
                            <div >
                                <button type="submit" >
                                    {{ __('Login') }}
                                </button>
                                <a  href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<a href="https://onlinecode.org/" target="_blank" alt="onlinecode" title="onlinecode">Free Download Example - onlinecode</a>
@endsection
Angular 6 CRUD Operations Application Tutorials

Read :

Another must read:  Laravel Multiple Authentication Example

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 onlinecode 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.

Hope this code and post will helped you for implement PHP Multiple Authentication using Laravel 5.7 Middleware – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  10  =  20

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US