简体   繁体   中英

Laravel 5 middleware Auth check not working

i want to show a 404 error page if the user try to access to de admin page if he is not logged or if he dont have the 'Admin' type. This work fine if the user is logged in, if not the user access to the admin panel. If i remove the if(Auth::check()) the script shows a Trying to get property of non-object error.

class AdminMiddleware {
    public function handle($request, Closure $next)
    {
        if(Auth::check()){
            if ($request->user()->type != 'Admin'){
                return abort(404);
            }
        }

        return $next($request);
    }

}

Try this

class AdminMiddleware {
    public function handle($request, Closure $next)
    {
        if(Auth::check()){
            if ($request->user()->type != 'Admin'){
                return abort(404);
            }
        }else{
            return abort(404);
        }
        return $next($request);
    }
}

It should check if user is logged in and if so check if hes and admin, if hes not logged in show him the 404

or a shorter version

class AdminMiddleware {
    public function handle($request, Closure $next)
    {
        if(Auth::check() && $request->user()->type == 'Admin'){
            return $next($request);
        }
        return abort(404);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM