Always got "message": "Unauthenticated." - Laravel Passport
If you are staring at a JSON response of { "message": "Unauthenticated." } after successfully exchanging credentials for an access token, it is frustrating because the logic seems sound. You have logged in via POST /oauth/token, received a valid bearer token, and your route is explicitly guarded by the auth:api middleware. The issue almost always lies not in the application's business logic, but in how the OAuth2 standard defines authorization headers versus how Laravel Passport validates them.
When you inspect the request using Advanced Rest Client (ARC), Postman, or similar tools, ensure that the Authorization header is formatted exactly as Bearer <your_token_here>. A common pitfall in ARC is that it sometimes includes extra whitespace or quotes depending on the version configuration, which breaks the strict string matching performed by the OAuth2 server. Additionally, verify that you have not accidentally included a Content-Type: application/json header if the request body was empty; while often tolerated, strict middleware configurations can interpret a non-empty body with missing parameters as a malformed request.
The core mechanism behind this failure involves the ResourceServer within Laravel Passport. When a request hits an endpoint protected by auth:api, the framework invokes the BearerTokenValidator. This validator checks three specific things in that order:
- Is the scheme
Bearer? - Does the token exist in the database (is it revoked or expired)?
- Does the client making the request have permission to access this resource?
Here is where many developers stumble. The OAuth2 flow separates the client (your application) from the resource owner (the user). When you log in, you are acting on behalf of a specific user. However, the BearerTokenValidator also checks if the client associated with that token has the necessary scopes to access the /user endpoint. If your client was registered without the required scope (e.g., read, write, or specifically User:Read), the validation fails, and Passport returns a generic "Unauthenticated" error rather than a specific "Insufficient Scope" message for backward compatibility reasons.
To resolve this, you must inspect the client_id associated with your token in the database (usually in the oauth_access_tokens table). Ensure that the personal_access_client or password_client flags are set correctly depending on whether you used personal access tokens or client credentials. More critically, check the scopes column of the token record. If the token was issued with empty scopes but your route requires specific ones, the validation will fail silently before reaching your controller logic.
Another frequent cause is the use of different clients for login and API calls. If you logged in using a "Personal Access Token" client but are trying to access the API with a standard "SPA" or "Web Application" client, the token might not match the expected client context during validation. Ensure that the client used to generate the token matches the client registered in your Passport client list.
Finally, if you are running into issues where the header looks correct but the error persists, check your Laravel logs carefully. As noted in similar cases, a deeper stack trace often reveals OAuthServerException: The resource owner or authorization server denied the request. This specific exception confirms that the token is recognized as invalid for the specific client attempting to access the resource. In such scenarios, regenerating the token with explicit scopes added via the Passport admin UI (if available) or updating the client's scope permissions in the database is often the quickest path to a working solution.
Remember that auth:api middleware relies on Laravel Passport's internal state. If you have not run php artisan passport:install after changing your application mode from laravel to passport, the necessary routes for token introspection and validation might be missing or misconfigured, leading to unexpected authentication failures. Always ensure that the .env file contains APP_ENV=passport (or testing) before generating new keys or tokens.