-1

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

Dever
  • 1
  • 3
  • you can just check what their role is after they authenticate and redirect them where you wish ... also your 2 middleware are identical (they are both admin middleware) – lagbox Sep 07 '22 at 22:37
  • ive reupdated the codes – Dever Sep 07 '22 at 22:43
  • which part of the auth controller should i touch when it concerns laravel breeze. That i think is the issue here – Dever Sep 07 '22 at 22:47

1 Answers1

0

If the admin never needs to go to /dashboard, when a user goes to /dashboard, you can redirect them to /admin if they are an admin user. Something like this

Route::get('/dashboard', function () {
    if(auth()->user()->role_as == 1){
        return redirect('/admin');
    }
    return view('dashboard');
})->middleware(['auth', 'user'])->name('dashboard');

If your admin does need to go /dashboard as well but you just want the admins starting place to be the admin page after logging in, you could add something like this to the login contoller (for me its in Controllers/Auth/AuthenticatedSessionController.php)

public function store(LoginRequest $request)
    {
        $request->authenticate();

        $request->session()->regenerate();
        if(auth()->user()->role_as == 1){
            return redirect()->intended('/admin');
        }
        return redirect()->intended(RouteServiceProvider::HOME);
    }
NotDavid
  • 161
  • 7
  • No like i just want a way the admin and the user can login and the admin should be redirected to his dashboard and the user to his dashboard. That is from the controller – Dever Sep 07 '22 at 23:11
  • Does the second option that I post work then? That is modifying the login controller for me. – NotDavid Sep 07 '22 at 23:36
  • 1
    Thanks but i think ive solved the issue already. What i did was to update the RedirectifAuthenticated file in the middleware folder with the login i am looking for and it worked. That might also help though. Thanks – Dever Sep 07 '22 at 23:40