如何重写 tokenGuard 中的 API_token 字段?

默认情况下,使用 token guard 需要在users表中添加 api_token 字段 进行查询,我想修改这个字段名,换成其他的,请问该如何覆写?谢谢。目前用的是lumen5.5

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 2
ibucoin

这个涉及比较多,给你个地址看看
文档
里面的话,主要是就是在__construct给$this->storageKey赋值为api_token了,所以在retrieveByCredentials的时候,会去数据库里面查找api_token
你直接继承一下这个TokenGuard,重写一下storageKey的赋值

在authProvider的里面,使用Auth::extend()返回自定义的Guard。
然后在config中的auth.php中指定就可以了。

Laravel的话,我大概就是这么用的,lumen就不是很清除了。

5年前 评论
小学毕业生 3年前
namespace Illuminate\Auth;

    /**
     * Create a token based authentication guard.
     *
     * @param  string  $name
     * @param  array  $config
     * @return \Illuminate\Auth\TokenGuard
     */
    public function createTokenDriver($name, $config)
    {
        // The token guard implements a basic API token based guard implementation
        // that takes an API token field from the request and matches it to the
        // user in the database or another persistence layer where users are.
        $guard = new TokenGuard(
            $this->createUserProvider($config['provider'] ?? null),
            $this->app['request'],
            $config['input_key'] ?? 'api_token',
            $config['storage_key'] ?? 'api_token',
            $config['hash'] ?? false
        );

        $this->app->refresh('request', $guard, 'setRequest');

        return $guard;
    }
3年前 评论

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