
How to authenticate admin users in Laravel ?
Created at 24-Feb-2022 ,
By samar
How to authenticate admin users in Laravel ?
Good day, guys. In this post, we’ll look at how to solve the "How to authenticate admin users in Laravel ?" programming puzzle.
To authenticate admin users you have to create an admin middleware. Now you have to add code in it to redirect the user to the dashboard if authenticated user type is admin. To get the user type you have to add a column in users table and on the basis of user type authenticate the users to dashboard in Laravel-
Create Admin middleware to authenticate admin users
//1. Create middleware to authenticate admin users using php artisan command</span></p> php artisan make:middleware Admin //2. Edit middleware Admin.php //app\Http\Middleware\Admin.php</strong></p> public function handle($request, Closure $next) { if (Auth::check() && Auth::user()->user_type == 'admin') { return $next($request); } else { return redirect('/'); } } //3. <span style="font-weight: 400;">Add it to the routeMiddleware array in your kernel file //app/http/Kernel.php protected $routeMiddleware = [ 'admin' => 'App\Http\Middleware\Admin', ]; //4. <span style="font-weight: 400;">Use Admin middleware to Route group //routes\web.php Route::middleware(['admin'])->group(function () { Route::get('admin/dashboard', '[email protected]')->name('admin.dashboard'); });
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to upload image in laravel 8
- How to validate form input data in laravel
- Laravel get all records with pagination
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Get id of last inserted record in laravel
- How to check record exist or not in relationship table
- Laravel specific table Migration
- Use of undefined constant laravel
- Input file with max size validation in laravel
- How to get specific columns using with method in laravel Eloquent relationship
- How to check records exist in loaded relationship in Laravel blade view
- How to get IP address in laravel
- How to prevent host header attack in Laravel
- Conditional validation in laravel
- Redirect to another view from controller in laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- Where to use whereNotNull eloquent in laravel
- Extra Filter Query on Relationships in Laravel
- Array to string conversion laravel blade
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Send id with route Laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Laravel create multiple records in Pivot table
- How to return a column with different name in Laravel
- First and last item of the array using foreach iteration in laravel blade