Home  >  Q&A  >  body text

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>
P粉362071992P粉362071992458 days ago482

reply all(1)I'll reply

  • P粉543344381

    P粉5433443812023-08-10 11:50:36

    For your question, one option available is to access policies. The scope of the sanctum component is shown in the following example.

    reply
    0
  • Cancelreply