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', 'AdminController@Index')->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
- First and last item of the array using foreach iteration in laravel blade
- Laravel append URI in route
- How to get specific columns using Laravel eloquent methods
- How to use or operator in laravel
- How to create and run user seeder in laravel
- How to get CSRF token in laravel controller
- How to get all route list
- Composer\Exception\NoSslException
- How to create new user without form submission in laravel
- Get last record from table in laravel
- Drop foreign key column in Laravel using migration
- Class 'App\Rules\Hash' not found in Laravel
- How to insert ckeditor data into database in Laravel?
- Composer create project laravel/laravel example app
- How to check data inserted or deleted in pivot after toggle method
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- How to add foreign key in laravel using migration
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Delete file from amazon s3 bucket using Laravel
- Send OTP using textlocal api in laravel
- Display success message in laravel
- How to check if user has created any post or not in laravel
- Laravel hasmany select not working
- Laravel 5.4 save data to database
- Laravel get count with where condition