LogoNavigate back to the homepage

Laravel passport prune / revoke token event listeners

Yish
December 26th, 2019 · 1 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:

1protected $listen = [
2 \Laravel\Passport\Events\AccessTokenCreated::class => [
3 \App\Listeners\RevokeOldTokens::class,
4 ],
5
6 \Laravel\Passport\Events\RefreshTokenCreated::class => [
7 \App\Listeners\PruneOldTokens::class,
8 ],
9];

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

  1. PruneOldTokens
  2. RevokeOldTokens
1public function handle(RefreshTokenCreated $event)
2{
3 DB::table('oauth_refresh_tokens')
4 ->where('id', '<>', $event->refreshTokenId)
5 ->where('access_token_id', '<>', $event->accessTokenId)
6 ->update(['revoked' => true]);
7}
1public function handle(AccessTokenCreated $event)
2{
3 DB::table('oauth_access_tokens')
4 ->where('id', '<>', $event->tokenId)
5 ->where('user_id', $event->userId)
6 ->where('client_id', $event->clientId)
7 ->update(['revoked' => true]);
8}

More articles from Yish

Creating .mdx file support and snippet in VSCode

Recently I used `.mdx` file to write my post, I found VSCode doesn't support `.mdx` by default. So how to do it to support mdx?

December 8th, 2019 · 1 min read

How to get client id from a request in Laravel passport?

For my situation, I need to get client id from per request and record it into the database, but how to get it from a request in laravel passport? There are many ways to do it.

December 3rd, 2019 · 1 min read
© 2019–2020 Yish
Link to $https://twitter.com/yishlaiLink to $https://github.com/Mombuyish