1

I am developing a middleware in order to handle redirect for guests to protected areas. I have 2 login pages, one for frontend and another one for backend and want the guests redirected to each login page depending on where they enter. I mean:

  • If the guest enters a route like /admin/dashboard they must be redirected to /admin/login.
  • If the guest enters route /messages they must be redirected to /login.

This is my routes.php

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

Route::group(['as'=>'clientRoutes', 'middleware'=>'loginSelector'], function(){
    Route::get('/polizas', function () {
        return view('cliente.polizas');
    });
});

Route::group(['as'=>'adminRoutes','prefix'=>'admin', 'middleware'=>'loginSelector'], function(){
    Route::get('/inicio', function () {
        return view('admin.inicio');
    });
    Route::get('/lista', function () {
        return view('admin.lista');
    });
});
Route::get('/admin/login', function () {
    return view('admin.login');
});

In the middleware I was trying to get group name of the route, but cant find the way to get i, something like this:

namespace App\Http\Middleware;

use Closure;

class CustomAuthRedirectMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $grupoRoute = $request->route()->getGroup();
        if($grupoRoute == "adminRoutes"){
            return redirect('/admin/login');
        }else{
            return redirect('/login');
        }

        return $next($request);
    }
}

If is possible how can I take the group name for doing the redirection? Or if it is impossible how can achieve this redirect?

Programador Adagal
  • 780
  • 14
  • 39

1 Answers1

1

I solved it thanks to the link provided by @Samsquanch.

This is the middleware code:

public function handle($request, Closure $next)
{
    $routeName = $request->route()->getName();
    if(strpos($routeName, 'adminRoutes.') === 0){
        if(!Auth::user()){
            return redirect('/admin/login');
        }
    }elseif(strpos($routeName, 'clientRoutes.') === 0){
        if(!Auth::user()){
            return redirect('/login');
        }
    }

    return $next($request);
}

Those are my routes:

Route::group(['as'=>'clientRoutes.', 'middleware'=>'loginSelector'], function(){
    Route::get('/polizas', function () {
        return view('cliente.polizas');
    });
});

Route::group(['as'=>'adminRoutes.','prefix'=>'admin', 'middleware'=>'loginSelector'], function(){
    Route::get('/inicio', function () {
        return view('admin.inicio');
    });
    Route::get('/lista', function () {
        return view('admin.lista');
    });
});
Route::get('/admin/login', function () {
    return view('admin.login');
});

When I go to url /admin/lista I get redirected to /admin/login, and when I got to /polizas I got redirected to /login.

If you use my solution you will have to take care what happens in each login form depending on authenticated user role.

Community
  • 1
  • 1
Programador Adagal
  • 780
  • 14
  • 39