关于 passport 生成新的 token 删除旧 token,事件监听报错?

用的laravel5.6.。再生成token时旧的token想删掉,看文档有一句file,于是我想加了句'Laravel\Passport\Events\AccessTokenCreated' => [
'App\Listeners\RevokeOldTokens',
],这个事件,生成了文件,file没写东西,我就打印一下,结果报错file这是什么原因呢??或者我想删除原来token,有什么方法呢??

颠倒的玉石
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

file

file

file

file

重新请求一下获取access_token接口,你会发现数据库中两个相对应的表只有一条最新数据,剩下的数据被清空了

5年前 评论
讨论数量: 7

同问,请问解决这个问题了吗?

5年前 评论

朋友,我已经解决这个问题了

5年前 评论

file

file

file

file

重新请求一下获取access_token接口,你会发现数据库中两个相对应的表只有一条最新数据,剩下的数据被清空了

5年前 评论
颠倒的玉石

@哈哈嘿嘿 谢谢啦

5年前 评论
第五焱陽

@哈哈嘿嘿 PruneOldTokens 中 貌似会把其他用户的 refresh_token 数据也清除了

5年前 评论

<?php

namespace App\Listeners;

use Laravel\Passport\Events\RefreshTokenCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;

class PruneOldTokens
{
/**

  • Create the event listener.
  • @return void
    */
    public function __construct()
    {
    //
    }

    /**

  • Handle the event.
  • @param RefreshTokenCreated $event
  • @return void
    */
    public function handle(RefreshTokenCreated $event)
    {
    //
    DB::table('oauth_refresh_tokens')
    ->where('id', '<>', $event->refreshTokenId)
    ->where('access_token_id', '<>', $event->accessTokenId)
    ->update(['revoked' => true]);

    }
    }

4年前 评论

<?php

namespace App\Listeners;

//use App\Events\Laravel\Passport\Events\AccessTokenCreated;
use Laravel\Passport\Events\AccessTokenCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;

class RevokeOldTokens
{
/**

  • Create the event listener.
  • @return void
    */
    public function __construct()
    {
    //
    }

    /**

  • Handle the event.
  • @param AccessTokenCreated $event
  • @return void
    */
    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]);

    }
    }

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!