Auth::check() at middleware

Posted by

error

!Auth::check() in the middleware to check if user is logged in or not, i am trying to get property of non-object but it shows an error like below: at => /Http/Middleware/VerifyCsrfToken.php $response->headers->setCookie

 admin middleware:

public function handle($request, Closure $next) { if (!Auth::check()) { return “Permission Denied.”; } return $next($request); }

Solution:

The error is encountering, “trying to get property of non-object,” suggests that the $response variable in the middleware is not an object as expected. To resolve this issue, you need to return an instance of a response object instead of a string. Here’s an updated version of your code:

Instead of returning a string, the middleware should return an instance of a response object. To achieve this, please consider the following approach:

public function handle($request, Closure $next) { if (!Auth::check()) { return response(‘Permission Denied.’, 401); } return $next($request); }
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x