Nested Middlewares

1

I'd like to allow access to a few pages only if the user is logged in.

I did so:

Route::group( ['middleware' => 'usuario'], function (){
    /* Minhas páginas */
});

So far so good, but I'd like to check if the password is already the current one (if sn_senha_atual == 'S' )

I thought about using nested middlewares, however it gives an ERR_TOO_MANY_REDIRECTS error when trying to use it like this:

Route::group( ['middleware' => 'usuario'], function (){
    Route::group( ['middleware' => 'senha'], function (){
      /*  minhas paginas */
    });
});

My user middleware looks like this:

 public function handle($request, Closure $next)
    {
        if( !Auth::check() ){
            return redirect()->back();
        }
            return $next($request);
    }

And my middleware password looks like this:

public function handle($request, Closure $next)
    {
        $user = Auth::user();
        if( $user->sn_senha_atual == 'N' ){
           // echo "Alterar id: ".$user->id;
            return redirect()->route( 'usu.alterar');
        }
        return $next($request);
    }

How do I check if the user is logged in, check if the password is the current one?

[EDIT 1]

Put the change page out of password middlware, but now it seems that user middleware does not let pass

    Route::group( ['middleware' => 'usuario'], function (){
            Route::group( ['middleware' => 'senha'], function (){
              Route::group(['as' => 'usu.', 'prefix' => 'usuario'], function (){
               Route::get('',['as' => 'index','middleware' => 'senha', 'uses' => 'UsuarioController@index']);
               Route::get('cadastrar',['as' => 'create', 'uses' => 'UsuarioController@create']);
               Route::post('salvar',['as' => 'store', 'uses' => 'UsuarioController@store']);
               Route::post('delete',['as' => 'remove', 'uses' => 'UsuarioController@remove']);
               Route::post('edit',['as' => 'edit', 'uses' => 'UsuarioController@edit']);
               Route::post('update',['as' => 'update', 'uses' => 'UsuarioController@update']);
               Route::get('sair',['as' => 'sair','middleware' => 'senha', 'uses' => 'UsuarioController@sair']);
               Route::get('logoff',['as' => 'logoff', 'uses' => 'UsuarioController@logOff']);
                 });
            });
           Route::post('alterar',['as' => 'alterar', 'uses' => 'UsuarioController@alterarSenha']);
        });
    
asked by anonymous 29.11.2017 / 19:54

1 answer

1

According to peer help and your friend's key comment Virgilio Novic

The route where the page was to change the password was inside the middleware password too, so I guess that was what was giving the other redirects ( ERR_TOO_MANY_REDIRECTS )

So it was like this:

Route::group( ['middleware' => 'usuario'], function (){
    Route::group( ['middleware' => 'senha'], function (){
       Route::group(['as' => 'usu.', 'prefix' => 'usuario'], function (){
            Route::get('alterar',['as' => 'alterar', 'uses' => 'UsuarioController@alterarSenha']);
         });
    });
});

Then it looks like this:

Route::group( ['middleware' => 'usuario'], function (){
    Route::group( ['middleware' => 'senha'], function (){
       Route::group(['as' => 'usu.', 'prefix' => 'usuario'], function (){

         });
    });
});

 Route::get('alterar',['as' => 'alterar', 'uses' => 'UsuarioController@alterarSenha']);

Also, according to documentation the middleware group can look like this:

Route::middleware(['first', 'second'])->group(function () {

or so:

Route::group( ['middleware' => ['usuario','senha']], function (){

Thanks for the help, friends

    
29.11.2017 / 21:23