Check user authentication status in controller, allow access to guest and user
<p>I'm using sanctum. I have a controller like this</p>
<pre class="brush:php;toolbar:false;">public function user()
{
if (auth('sanctum')->check()) {
return true;
} else {
return false;
}
}</pre>
<p>The problem is that without the middleware <code>auth:sanctum</code>, the conditional statement always returns false, and once I use this middleware, everything works fine. However, I cannot use this middleware because then the user would have to be logged in, and I want to give both guests and users access. How can I do this?
I would like to check in the controller if the user is logged in and if so, for example, be able to return the user's id. </p>