#Unauthenticated response

1 messages · Page 1 of 1 (latest)

tardy vine
#

Hi guys, could anyone help me with an authentication issue? Whenever I send a POST request from another app to my Laravel app, I get back {"message": "Unauthenticated."}. I'm running Laravel 8, using sanctum and attaching a generated PAT to my request as a bearer token.
The route is under the auth:sanctum middleware.

I tracked it to a point where it looks like the request is checked for an attached user, which is not there.

barren heart
#

Check if the received request contains the auth header. If it's a cross-origin ajax request and the header isn't allowed in the cors config it will be removed.

tardy vine
#

Could you point me to where it gets stripped off? I still see it at sendRequestThroughRouter().

barren heart
#

That would be the HandleCors middleware

tardy vine
#

Don't know if its relevant, but my cors.php contains 'allowed_headers' => ['*']

barren heart
#

Is the domain you're sending the request from allowed in config/sanctum.php?

#

Sorry, that's only relevant for stateful auth, not bearer token.

tardy vine
#

I do need to mention there are parts of the app not made by me

tardy vine
barren heart
#

The actual authentication happens in Laravel\Sanctum\Guard. I would check there to see what's wrong.

#

Could also be that you're simply using the wrong token 😉

tardy vine
#

I checked the token, it should be correct

#

It doens't even seem to reach the __invoke of Guard 😦

#

I don't understand, it's not even checking the token

#

Could my route be under the wrong middleware?

#

Is the token in the request supposed to be linked to a user at a later time? If so, does anyone know where?

barren heart
#

Does the route use the auth:sanctum middleware?

tardy vine
#

Yes

#

Is auth:sanctum only meant for requests containing users?

barren heart
#

It's meant for all non-public routes. The token belongs to a User model (which uses the HasApiTokens trait). So the sanctum guard identifies the user from the bearer token.

#

Try running artisan route:list to see all the middleware used by that route.

tardy vine
#

Hi Mono, sorry for the late reply, I went down kind of a rabbithole

#

So route:list points me here:
App\Http\Middleware\Authenticate:sanctum

#

Except I can't find what that refers to

#

I finally figured out I'm supposed to reach actingAs in my Sanctum.php, but my request keeps getting rejected before that even happens

#

It's super frustating, I can see the only guard that's being checked is Sanctum and yet

#

In my auth.php I have this:
'sanctum' => [
'driver' => 'session',
'provider' => 'users',
],