Search code examples
phplaravellaravel-passport

Laravel: Authenticate an Authenticatable Model statelessly


I need help.

Current situation, I have a Company model. That company model refers to a Client Credentials grant OAuth Client (passport). I managed to make a middleware that resolves the current Company from request via the passed bearer token of the API request.

The problem: I want to set that Company instance as the currently authenticated user statelessly. In the docs, there is a once() method from the Auth facade. (https://laravel.com/docs/7.x/authentication#other-authentication-methods)
However, the once() only accepts credentials, not an Authenticatable instance.

Is there a similar method to Auth::once() that accepts an Authenticatable instance instead of credentials?


Solution

  • Turns out, in a stateless endpoint (e.g. passport protected routes), Auth::login() doesn't store any cookie/session because it can't.

    So just use Auth::login() directly.