skip to Main Content

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


  1. for your question, one option you can use is the access policies, the scope of the sanctum package is shown in the following example

    Login or Signup to reply.
  2. It could be a session problem, didn’t you forget the Sanctum’s EnsureFrontendRequestsAreStateful middleware ?
    https://laravel.com/docs/10.x/sanctum#sanctum-middleware

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search