Tmeister / wp-api-jwt-auth

A simple plugin to add JSON Web Token (JWT) Authentication to WP REST API
GNU General Public License v2.0
551 stars 159 forks source link

jwt_auth_token_before_dispatch not working as expected #154

Closed avks closed 5 years ago

avks commented 5 years ago

I am trying to fire wp_login action with jwt_auth_token_before_dispatch filter. The code I am using is something like this:

 function mod_jwt_auth_token_before_dispatch($data, $user) {
     do_action('wp_login', $user->data->username, $user);
     return $data;
 }
 add_filter('jwt_auth_token_before_dispatch', 'mod_jwt_auth_token_before_dispatch');

Although the doc specifies $data as the only argument, I see from the source code that the filter can accept two parameters: https://github.com/Tmeister/wp-api-jwt-auth/blob/a67b833992ab82c2b8fe29210a9bad03623b5045/public/class-jwt-auth-public.php#L165

But this doesn't work as expected. Instead, I found in /var/log/apache2/error.log:

[Mon Apr 15 11:42:07.347592 2019] [php7:error] [pid 31616] [...] PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function mod_jwt_auth_token_before_dispatch(), 1 passed in /var/www/.../wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /var/www/.../hooks.php:54\nStack trace:\n#0 /var/www/.../wp-includes/class-wp-hook.php(288): 
mod_jwt_auth_token_before_dispatch(Array)\n#1 /var/www/.../wp-includes/plugin.php(208):
 WP_Hook->apply_filters(Array, Array)\n#2 /var/www/.../wp-content/plugins/jwt-authentication-for-wp-rest-api/public/class-jwt-auth-public.php(165): 
apply_filters('jwt_auth_token_...', Array, Object(WP_User))\n#3 /var/www/.../wp-includes/rest-api/class-wp-rest-server.php(946): 
Jwt_Auth_Public->generate_token(Object(WP_REST_Request))\n#4 /var/www/.../wp-includes/rest-api/class-wp-rest-server.php(329): WP_REST_Server->dispatch(Object(WP_RE in /var/www/.../hooks.php on line 54, referer: http://localhost:3000/login

Does anyone have a clue what might be causing this?

avks commented 5 years ago

My bad. Should call add_filter with two more arguments

 add_filter('jwt_auth_token_before_dispatch', 'mod_jwt_auth_token_before_dispatch', 10, 2);