Check user authentication status in controller, allow access to guest and user
P粉362071992
P粉362071992 2023-08-09 13:04:44
0
1
435
<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粉362071992
P粉362071992

reply all(1)
P粉543344381

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!