Laravel 10 Tutorial #6 | Create Admin Middleware to Protect Admin Routes | Multi Authentication

preview_player
Показать описание

Welcome to Part-6 of the Laravel 10 Tutorial in which we are first creating the admin panel. In this video, we will work on Multi Authentication in which we will use Guards for Admins and default Laravel Auth for Users.

We will follow the below steps to set Guards for Admin Panel / Admins.

1) Create admins table:-
First of all, we will create an admins table with migration with the below columns:
id, name, type, mobile, email, password, image, status

So, we will run the below artisan command to create a migration file for admins:-
php artisan make:migration create_admins_table

Open the create_admins_table migration file and add all required columns mentioned earlier.

Now we will run below artisan command to create an admins table with the required columns:-
php artisan migrate

2) Create Admin model:-
Now we will create an Admin model with the below artisan command:-
php artisan make:model Admin

We will update the content of the Admin model file to set the protected guard variable for admin and set other variables as shown in the video.

We will also extend the Admin class to Authenticatable and add its namespace as well.

We will also set providers for admins to assign eloquent in driver and Admin class in model.

4) Create Admin Middleware :-
Now we will create Admin Middleware file by running below command :-
php artisan make:middleware Admin

6) Update Admin Middleware
Add Auth:guard check in Admin Middleware to protect the admin routes. This check will be false for now as we have not registered the admin guard yet.

Add admin middleware group and move admin dashboard route under it to protect it from unauthorized access.

Now no one can access admin dashboard without login into the admin panel. We have used Guards to protect the admin routes including dashboard route.
In next video, we will work on admin login and logout functionality. We will register admin guard every time when admin logged in and destroy it every time when admin logged out from the admin panel.

Thanks for watching :)

Popular Stack Developers Series that can help you:-

►Join this channel to get the complete source code of all series:

Follow Stack Developers on Social Media to get updates and resolve your queries

#laravel10 #laravel10tutorial #laravel
Рекомендации по теме
Комментарии
Автор

And also make a video on a role base auth system

oscarpeter
Автор

10:49 there is an error on my Admin.php file. When my cursor move above the word !Auth it showed "Undefined type 'Auth'.intelephense(1009)"
here is my code:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use
use Auth;

class Admin
{
/**
* Handle an incoming request.
*
* @param $next
*/
public function handle(Request $request, Closure $next): Response
{

return redirect('/admin/login');
}
return $next($request);
}
}
can you help me, please?

ervanrhermawan
Автор

When I type admin/dashboard in url, it redirects me to auth/login, I do not know why it doesn't redirect to admin/login, and I have done all the steps in the video

karimahmedhassan
Автор

sir what project are we developing in this series ?

roopacefrinz
Автор

(){
Route::get('dashboard', [AdminController::class, 'dashboard']);

});
working ...

mohamedazeemansar