Problems with Middlwares - Laravel 5.4

Asked

Viewed 54 times

0

I am using a middleware on Laravel 5.4 and would like if the logged in user had permission 2 to be redirected to /admin/signups. The scope of the Handle method looks like this, but the redirect doesn’t work. Any idea?

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AdminMiddleware{

    public function handle($request, Closure $next){

        // SE O USUÁRIO NÃO ESTIVER LOGADO REDIRECIONA PARA O LOGIN
        if (!Auth::check()) {
            return redirect('login');
        }else{
            // SE O USUÁRIO QUE LOGOU FOR CANDIDATO REDIRECIONA PARA MINHA CONTA
            if($request->user()->permissao !== 2){
                return redirect('/minhaconta');
            }elseif($request->user()->permissao === 2){
                return redirect('/admin/inscricoes');
            }else{
                return $next($request);
            }
        }
    }
}
  • Where did you register middleware in the app? It’s on some route or all requests?

  • What happens, where it’s set up?

  • @Andersoncarloswoss I have registered in the routes that have the prefix 'admin'. Think I should register in all?

  • Already tried to use operators != and ==? I think the value is coming as a string and thus is different from an integer.

  • You’re doing it for the logged-in right? exchange the $request by the auth()->user()->permission helper, also put some dd() so we know what’s going on.

1 answer

0

Dude, I believe the problem is in your class, because it is missing the instance of Redirectresponse, which contains the properties of headers needed for redirection, try to add this call.

Will stay like this:

use Illuminate Support Facades Auth;

use Illuminate Http Redirectresponse

  • In the documentation there are several examples using the helper redirect without using the class RedirectResponse, so I guess that’s not the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.