【问题标题】:How to change JSON response on JWT Authentication for WP-API?如何更改 WP-API 的 JWT 身份验证的 JSON 响应?
【发布时间】:2019-09-22 02:42:45
【问题描述】:

我已使用“JWT Authentication for WP-API”插件登录并生成令牌。

在 JSON 响应中,我只有 wp_users 表中的一些数据。

如何更改它以在响应时从wp_usermeta 表中获取一些其他值,因为我想知道用户状态级别。

【问题讨论】:

    标签: php wordpress jwt jwt-auth


    【解决方案1】:

    您需要使用过滤器jwt_auth_token_before_signjwt_auth_token_before_dispatch

    第一个过滤器接收令牌数据和一个用户对象。

    在另一个插件或您的主题中,您需要调用add_filter

    我的 Wordpress 有点生疏,我没有方便的 WP 实例来测试它,但这是一般理论:

    基本示例:

    add_filter('jwt_auth_token_before_sign', 'add_user_info_jwt', 10, 2);
    
    function add_user_info_jwt($token, $user) {
    
        // fetch whatever information you want from the user, probably using the $user
        // object as starting point.
    
        $token['roles'] = implode(',', $user->roles);;
    
        return $token;
    }
    
    

    您收到的$token 将具有以下起始结构和信息:

    $token = [
                'iss'  => get_bloginfo( 'url' ),
                'iat'  => $issuedAt,
                'nbf'  => $notBefore,
                'exp'  => $expire,
                'data' => [
                    'user' => [
                        'id' => $user->data->ID,
                    ]
                ]
    ];
    

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 2016-10-06
      • 2021-10-08
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2017-03-09
      相关资源
      最近更新 更多