Today, we are going to how to create multiple authentications using the laravel 6(Laravel 6 Multiple Authentication Example Tutorial). so we will give you a simple example of how to create Multiple Authentication Example in laravel 6.
Overview
Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Create Table using migration
Step 4: Install the Laravel/UI package
Step 5: Install the Laravel Auth command
Step 6: Create IsAdmin Middleware
Step 7: Modify the Controllers and Model
Step 8: Define The Route
Step 9: Create Blade Files
Step 1: Install Laravel
We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
composer create-project --prefer-dist laravel/laravel laravel6_multi_auth
Step 2: Setting Database Configuration
After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(larave6_multi_auth) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root)
Step 3: Create Table using migration
Now, We need to update users migration table. so we will update the users migration table, see below file in update the code for users table.
bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->boolean('is_admin')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } ?>
Run the below command. after the changes above file.
php artisan migrate
Step 4: Install the Laravel/UI package
We need to laravel UI package so we will install the package using the below command.
composer require laravel/ui
when completed successfully installation of laravel UI package then we will see look like as below type of output.
./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing laravel/ui (v1.0.1): Downloading (100%) Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully.
Step 5: Install the Laravel Auth command
Now, we will install the laravel authentication using below command.
php artisan ui bootstrap --auth
here, Laravel extracted into a scaffolding separate laravel UI packages.
Bootstrap scaffolding installed successfully. Please run "npm install && npm run dev" to compile your fresh scaffolding. Authentication scaffolding generated successfully.
Step 6: Create Middleware
Now, we will create IsAdmin Middleware using below command and we need to some changes in handle method. so you can see below code.
php artisan make:middleware IsAdmin
app/Http/Middleware/IsAdmin.php
user()->is_admin == 1){ return $next($request); } return redirect('home')->with('error',"You don't have admin access."); } } ?>
After complete changes. we need to assign route on routeMiddleware array in app/Http/Kernel.php file. so you can see below code.
app/Http/Kernel.php
/** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'is_admin' => \App\Http\Middleware\IsAdmin::class, ];
Step 7: Modify the controller and model
Now here, we need to add adminHome() method in HomeController.php file.
app/Http/Controllers/HomeController.php
middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } /** * Show the application dashboard for admin. * * @return \Illuminate\Contracts\Support\Renderable */ public function adminHome() { return view('adminHome'); } } ?>
And second we need to update the LoginController.php file. so you can follow below code.
app/Http/Controllers/Auth/LoginController.php
middleware('guest')->except('logout'); } public function login(Request $request) { $input = $request->all(); $this->validate($request, [ 'email' => 'required|email', 'password' => 'required', ]); if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password']))) { if (auth()->user()->is_admin == 1) { return redirect()->route('admin.home'); }else{ return redirect()->route('home'); } }else{ return redirect()->route('login') ->with('error','Email-Address And Password Are Wrong.'); } } } ?>
app/User.php
'datetime', ]; } ?>
Step 8: Define The Route
We will open the web.php in the routes directory and paste below following code.
name('home'); Route::get('admin/home', 'HomeController@adminHome')->name('admin.home')->middleware('is_admin'); ?>
Step 9: Create Blade Files
here in this step, we need to create a new adminHome.blade.php file or you can copy file of home.blade.php and change the file name to the adminHome.blade.php.
adminHome.blade.php
@extends('layouts.app') @section('content')@endsectionDashboard@if (session('status')){{ session('status') }}@endif You are logged in admin dashboard!
Really nice design and excellent articles, absolutely nothing else we want : D.