{"id":536,"date":"2023-05-26T22:49:00","date_gmt":"2023-05-26T22:49:00","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=536"},"modified":"2023-05-26T11:19:15","modified_gmt":"2023-05-26T11:19:15","slug":"authcheck-at-middleware","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/authcheck-at-middleware\/","title":{"rendered":"Auth::check() at middleware"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"208\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-48-edited.png\" alt=\"\" class=\"wp-image-538\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-48-edited.png 479w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-48-edited-300x130.png 300w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/figure>\n\n\n\n<p>error<\/p>\n\n\n\n<p><code>!Auth::check()<\/code>\u00a0in 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 <\/p>\n\n\n\n<p>\u00a0admin middleware:<\/p>\n\n\n\npublic function handle($request, Closure $next)\n{\n    if (!Auth::check()) {\n        return &#8220;Permission Denied.&#8221;;\n    }\n    return $next($request);\n}\n\n\n\n<p>Solution:<\/p>\n\n\n\n<p>The error is encountering, &#8220;trying to get property of non-object,&#8221; suggests that the <code>$response<\/code> 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&#8217;s an updated version of your code:<\/p>\n\n\n\n<p>Instead of returning a string, the middleware should return an instance of a response object. To achieve this, please consider the following approach:<\/p>\n\n\n\npublic function handle($request, Closure $next)\n{\n    if (!Auth::check()) {\n        return response(&#8216;Permission Denied.&#8217;, 401);\n    }\n    return $next($request);\n}\n","protected":false},"excerpt":{"rendered":"<p>error !Auth::check()\u00a0in 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&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/536\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}