I was facing issues of making user and admin role system with laravel breeze. It was quick and straight forward with laravel ui authentication.
So i created a middleware for the admin and user and registered it in the kernel.php file as usual.
I also wrote the logic for the code on both the middlewares.
But the issue now is to figure out how i can configure their login controller for it to work effectively as it should. Code Display
Adminmiddleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class Adminmiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if(Auth::check()){
if(Auth::user()->role_as == '1'){
return $next($request);
}else{
return redirect('/home')->with('message','You are not an admin');
}
}else{
return redirect('/login')->with('message','please log in');
}
}
}
User middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class Usermiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if(Auth::check()){
if(Auth::user()->role_as == '0'){
return $next($request);
}else{
return redirect('/home');
}
}else{
return redirect('/login')->with('message','please log in');
}
}
}
Kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::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' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'user' => \App\Http\Middleware\Usermiddleware::class,
'admin' => \App\Http\Middleware\Adminmiddleware::class,
];
Web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
// Route::get('/dashboard', function () {
// return view('dashboard');
// })->middleware(['auth'])->name('dashboard');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'user'])->name('dashboard');
// ADMIN DASHBOARD
Route::get('/admin', function () {
return view('admin.admindashboard');
})->middleware(['auth', 'admin'])->name('admin_dashboard');
require __DIR__.'/auth.php';
Now i created a migration file to add another field for the users table i.e the role_as row in the table and gave it a default value of 0. 0 is for users 1 is for admin
Now i want a way to make the admin and users login to their respective dashboard from the auth controller