Skip to content

Commit 86aadf3

Browse files
authored
Merge pull request #42 from wp-graphql/bug/#41-jwt-fields-not-retrieved-via-viewer-query
#41 - JWT fields cannot be retrieved via viewer query
2 parents f18e999 + ce4f5b2 commit 86aadf3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/ManageTokens.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WPGraphQL\JWT_Authentication;
44

55
use GraphQL\Error\UserError;
6+
use WPGraphQL\Model\User;
67
use WPGraphQL\Types;
78

89
class ManageTokens {
@@ -85,7 +86,9 @@ public static function add_user_fields( $fields ) {
8586
$fields['jwtAuthToken'] = [
8687
'type' => Types::string(),
8788
'description' => __( 'A JWT token that can be used in future requests for authentication/authorization', 'wp-graphql-jwt-authentication' ),
88-
'resolve' => function ( \WP_User $user ) {
89+
'resolve' => function ( User $user ) {
90+
91+
$user = get_user_by( 'id', $user->userId );
8992

9093
/**
9194
* Get the token for the user
@@ -106,7 +109,9 @@ public static function add_user_fields( $fields ) {
106109
$fields['jwtRefreshToken'] = [
107110
'type' => Types::string(),
108111
'description' => __( 'A JWT token that can be used in future requests to get a refreshed jwtAuthToken. If the refresh token used in a request is revoked or otherwise invalid, a valid Auth token will NOT be issued in the response headers.', 'wp-graphql-jwt-authentication' ),
109-
'resolve' => function ( \WP_User $user ) {
112+
'resolve' => function ( User $user ) {
113+
114+
$user = get_user_by( 'id', $user->userId );
110115

111116
/**
112117
* Get the token for the user
@@ -127,12 +132,12 @@ public static function add_user_fields( $fields ) {
127132
$fields['jwtUserSecret'] = [
128133
'type' => Types::string(),
129134
'description' => __( 'A unique secret tied to the users JWT token that can be revoked or refreshed. Revoking the secret prevents JWT tokens from being issued to the user. Refreshing the token invalidates previously issued tokens, but allows new tokens to be issued.', 'wp-graphql' ),
130-
'resolve' => function ( \WP_User $user ) {
135+
'resolve' => function ( User $user ) {
131136

132137
/**
133138
* Get the user's JWT Secret
134139
*/
135-
$secret = Auth::get_user_jwt_secret( $user->ID );
140+
$secret = Auth::get_user_jwt_secret( $user->userId );
136141

137142
/**
138143
* If the secret cannot be returned, throw an error
@@ -319,11 +324,11 @@ public static function add_tokens_to_graphql_response_headers( $headers ) {
319324
* @throws \Exception
320325
*/
321326
public static function add_auth_headers_to_rest_response( $response, $handler, $request ) {
322-
327+
323328
if( ! $response instanceof \WP_HTTP_Response ) {
324329
return $response;
325330
}
326-
331+
327332
/**
328333
* If the request _is_ SSL, or GRAPHQL_DEBUG is defined, return the tokens
329334
* otherwise do not return them.

0 commit comments

Comments
 (0)