laravel / passport

Laravel Passport provides OAuth2 server support to Laravel.
https://laravel.com/docs/passport
MIT License
3.25k stars 780 forks source link

[12.x] Create access and refresh token in `Passport::actingAs()` #1740

Closed gdebrauwer closed 2 months ago

gdebrauwer commented 2 months ago

If you use Passport::actingAs() in a test while in the tested API route needs to revoke the authenticated user's current access token, then that causes an error in the test. That is because Eloquent can not revoke a token that does not exist in database.

Passport::actingAs($user);

auth()->user()->token()->revoke(); // This currently causes an error

This PR fixes that by creating and saving an access and refresh token in the database.

I also create a refresh token because of #1739 so the following works as well:

Passport::actingAs($user);

auth()->user()->token()->refreshToken->revoke();
taylorotwell commented 2 months ago

This might be a breaking change to existing applications. I would recommend maybe creating the tokens yourself in the few tests that need to actually revoke. 👍