phplaravelauthenticationlaravel-sanctum

Laravel Sanctum Token API Authentication Not Working in Postman


I am trying to utilize Sanctum for an API only application. I am not using it for a SPA. I have a single end point set up and protected by the Sanctum middleware. I am creating a user and a token for said user through the tinker cli tool. I then paste the token into Postman under the bearer token selection within the authorization tab. However, when I submit the request I get an unauthenticated error. Not quite sure what I am doing incorrect here. Followed the documentation provided very closely as well as the sparse videos I could find. Here are some code snippets. I appreciate the insight here.

API.php

Route::middleware('auth:sanctum')->apiResource('/documents','DocumentHandlerController');

Middleware/Authenticate.php

class Authenticate extends Middleware
{
    /**
     * Return 401 when not authorized
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    public function handle($request)
    {
        return response()->json(
            ['message'=>'Unauthorized']
            ,Response::HTTP_UNAUTHORIZED
        );
    }
}

Function from my controller

    public function index()
    {
        return response()->json(['Success'],Response::HTTP_OK);
    }

Solution

  • I had a similar issue where I got the response {"message":"Unauthenticated."}.

    For me it was because in my .env file, my APP_URL was https://www.example.com and I was trying to access the API via the URL https://example.com.

    When I changed the URL that made the API call to https://www.example.com it was working.