2

I made a custom route middleware to check if my user has enough privileges to see a page, when user doesn't have enough privileges I get the following error message:

 Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function setCookie() on null

This is my controller:

<?php
namespace App\Http\Middleware;


use Closure;
use Illuminate\Http\Request;
use Auth;



class AuthAdmin
{


    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if($user && $user['privileges'] > 2){
            return $next($request);   
        }

        return view('auth.login');

    }
}

Any idea what's wrong with my code?

gabogabans
  • 3,035
  • 5
  • 33
  • 81
  • Possible duplicate of [Call to a member function setCookie() on null](https://stackoverflow.com/questions/48970172/call-to-a-member-function-setcookie-on-null) – newUserName02 Mar 19 '19 at 19:48
  • 2
    Don't return a view from middleware, return a redirect response: https://stackoverflow.com/questions/50408698/laravel-return-view-on-middleware – newUserName02 Mar 19 '19 at 19:54

2 Answers2

0

try

return response()->view('auth.login');

-1

Instead of return view('Somepage') try return redirect('some route')

Dinesh Kumar
  • 131
  • 3
  • 11