TypeError from throttle requests when doesn't pass the middleware

Viewed 770

I didn't know how to describe the subject but here's my problem:

I have a model/table named users, and it has a field called type. I made a middleware to give controller access to the specific types. when it passes the middleware, there's no problem. but when the type is not the same, it gives an error like this:

Argument 1 passed to Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders() must be an instance of Symfony\\Component\\HttpFoundation\\Response, string given, called in C:\\xampp\\htdocs\\app\\Http\\Middleware\\ThrottleRequests.php on line 3

the thing is, this is giving me a typeError, and it's not the type field I have in my table. I tried to change the field name in case you think this is about my field name.

ps: I didn't do anything else, it's just the default class I didn't try to modify the throttle middleware.

ps: the middlewares I'm using are:

'support' => [
    'auth:api',
    'customMiddleware',
]

UPDATE:

customeMiddleware:

 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    $user = Auth::user();
    $AccessLevel = 2;
    if($user->type >= $AccessLevel){
        return $next($request);
    }

    return "UnAuthorized";
}
2 Answers

You probaly automatically imported the wrong response class.

In ThrottleRequests.php

You probably have:

use Symfony\Component\HttpFoundation\Response;

replace it with:

use Illuminate\Http\Response;

Sharing your code will help, people answer indeed.

Don't return a string, that is not mathing the expected return type:

 throw new UnauthorizedHttpException('Some message')

Your issue is that you are simply returning a string from the middleware. I can't remember if abort() works in middleware, but try this:

 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    $user = Auth::user();
    $AccessLevel = 2;
    if($user->type >= $AccessLevel){
        return $next($request);
    }

    return abort(403);
}
Related