I am using sanctum. I have such a controller
public function user()
{
if (auth('sanctum')->check()) {
return true;
} else {
return false;
}
}
The problem is that without middleware auth:sanctum
, the conditional statement always returns false, and as soon as I use this middlewate, everything works fine. However, I can’t use this middleware, because then the user will have to be logged in, and I would like to give access, both to guests and users. How can I do this?
I would like to check in the controller if the user is logged in, and if so, be able to return, for example, the id of that user.
2
Answers
for your question, one option you can use is the access policies, the scope of the sanctum package is shown in the following example
It could be a session problem, didn’t you forget the Sanctum’s
EnsureFrontendRequestsAreStateful
middleware ?https://laravel.com/docs/10.x/sanctum#sanctum-middleware