Skip to content
Yish

Laravel passport prune / revoke token event listeners

Laravel1 min read

I found serveral examples, but I used two examples and sort up it, here is solution:

  • stack overflow
  • official

Firstly, you need to add two classes to EventServiceProvider:

protected $listen = [
\Laravel\Passport\Events\AccessTokenCreated::class => [
\App\Listeners\RevokeOldTokens::class,
],
\Laravel\Passport\Events\RefreshTokenCreated::class => [
\App\Listeners\PruneOldTokens::class,
],
];

Next, execute php artisan event:generate artisan command, it should be generating two event classes:

  1. PruneOldTokens
  2. RevokeOldTokens
public function handle(RefreshTokenCreated $event)
{
DB::table('oauth_refresh_tokens')
->where('id', '<>', $event->refreshTokenId)
->where('access_token_id', '<>', $event->accessTokenId)
->update(['revoked' => true]);
}
public function handle(AccessTokenCreated $event)
{
DB::table('oauth_access_tokens')
->where('id', '<>', $event->tokenId)
->where('user_id', $event->userId)
->where('client_id', $event->clientId)
->update(['revoked' => true]);
}
© 2021 by Yish. All rights reserved.