Skip to content
Advertisement

How to redirect Admin to a specific page and users to another – Laravel 8 jetstream

I’m trying to redirect admin to the admin panel page and the users to the homepage

i created new field for usertype and i add the value in database which is “admin”

$table->string('usertype')->nullable();

then i created this Midlleware (AdminMiddleware):

<?php

 namespace AppHttpMiddleware;

 use Closure;
 use IlluminateHttpRequest;
 use IlluminateSupportFacadesAuth;

 class AdminMiddleware
{
/**
 * Handle an incoming request.
 *
 * @param  IlluminateHttpRequest  $request
 * @param  Closure  $next
 * @return mixed
 */
public function handle(Request $request, Closure $next)
{
    if(Auth::user()->usertype == 'admin')
    {
        return $next($request);
    }
    else
    {
        return redirect('/dashboard');
    }
    
   }
  }

then i add it to Kernel:

        'admin' => AppHttpMiddlewareAdminMiddleware::class,

and i add the function to RouteServiceProvider and imported the class:

public const HOME = '/dashboard';
    protected function redirectTo()
    {
        if(Auth::user()->usertype == 'admin')
        {
            return 'panel';
        }
        else
        {
            return 'dashboard';
        }
    }

then i added the route to web.php:

Route::get('/', function () {
return view('index');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('index');
})->name('dashboard');





Route::group(['middleware' => ['auth:sanctum','auth','admin','verified']],function() {
Route::get('/panel', function () {
    return view('admin.dashboard');
});

});

and i created two accounts one of them is the admin with value of “admin” for usertype but when i login it still redirecting me to the homepage

what am i doing wrong here?

Advertisement

Answer

There is a simple way which I have been using in my app.

You don’t need a middleware for this purpose. Create a controller in the name of HomeController or whatever you want. In you LoginController change the property redirectTo to a simple route. For example ‘/home’ or even / for super simple routes. Now in routes/web.php add this route pointing to a method in HomeController.

Route::get('/home', 'HomeController@redirectUser');

Or

Route::get('/', 'HomeController@redirectUser');

Additionally create your own routes which you want to redirect to.

Route::group(['middleware' => ['auth:sanctum', 'verified']], function () {
    Route::get('/panel', function () {
        return view('admin.dashboard');
    });
});

 You can also create routes for number of users.

Now in your HomeController, in redirectUser() method, add your needs.

if (auth()->user()->usertype == 'admin') {
    return redirect(url('route url'));
} else {
    return redirect (url('some other url'));
}

Please make sure that you have defined the same URI in your routes file.

You can also redirect users in your LoginController as well. But it works only after logging in. this method will also redirects user to the desired location when they click home button or the logo which contains homepage url.

Hope this one will help you.

Thank you.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement