Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tyfc's avatar
Level 4

Testing passport protected API

Dear Laravel Folks

I have just set up a small API that is protected via Passport. So far, everything is working well.

Since I will be the only one consuming the API, I simply use \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class in order to generate and handle my API tokens.

To my question: What is the best way to handle tokens in my application tests? actingAs($user) does not seem to magically add the user's token to the request. I can use $this->withoutMiddleware();, but that feels kind of dirty.

Any ideas or best practices?

Thank you very much for you help!

0 likes
4 replies
tyfc's avatar
Level 4

Is there any change someone has an idea? Every pointer would be very much appreciated. :-)

Kaiser's avatar

Yes, I'm having the same issue. It would be nice a lesson on testing on password.

tyfc's avatar
tyfc
OP
Best Answer
Level 4

This turned out to be much easier than expected, and was even covered in the documentation. A simple $this->actingAs($user, 'api') does the trick!

14 likes

Please or to participate in this conversation.