Skip to content

Commit d4ba4eb

Browse files
committed
DO NOT MERGE
- This is an initial attempt at addressing the conflict. This is not ready for merge, but wanted to test to see if the general approach fixes the conflict for @mhinton
1 parent f18e999 commit d4ba4eb

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ protected static function authenticate_user( $username, $password ) {
364364
* @param (int|bool) $user Logged User ID
365365
*
366366
* @return mixed|false|\WP_User
367+
* @throws \Exception
367368
*/
368369
public static function filter_determine_current_user( $user ) {
369370

wp-graphql-jwt-authentication.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
* @package WPGraphQL_JWT_Authentication
1818
*/
1919

20-
namespace WPGraphQL\JWT_Authentication;
20+
namespace WPGraphQL\JWT_Auth;
2121

2222
// If this file is called directly, abort.
23+
use WPGraphQL\JWT_Authentication\Auth;
24+
use WPGraphQL\JWT_Authentication\ManageTokens;
25+
2326
if ( ! defined( 'WPINC' ) ) {
2427
die;
2528
}
@@ -33,7 +36,7 @@
3336
require_once( 'c3.php' );
3437
}
3538

36-
if ( ! class_exists( '\WPGraphQL\JWT_Authentication' ) ) :
39+
if ( ! class_exists( '\WPGraphQL\JWT_Auth' ) ) :
3740

3841
final class JWT_Authentication {
3942

@@ -175,7 +178,7 @@ private static function init() {
175178
add_filter( 'determine_current_user', [
176179
'\WPGraphQL\JWT_Authentication\Auth',
177180
'filter_determine_current_user'
178-
], 10, 1 );
181+
], 1, 1 );
179182

180183
/**
181184
* Filter the rootMutation fields
@@ -202,4 +205,43 @@ function init() {
202205
return JWT_Authentication::instance();
203206
}
204207

205-
add_action( 'plugins_loaded', '\WPGraphQL\JWT_Authentication\init' );
208+
add_action( 'plugins_loaded', '\WPGraphQL\JWT_Auth\init', 1 );
209+
210+
add_filter( 'determine_current_user', function( $user ) {
211+
212+
/**
213+
* Validate the token, which will check the Headers to see if Authentication headers were sent
214+
*
215+
* @since 0.0.1
216+
*/
217+
$token = Auth::validate_token();
218+
219+
/**
220+
* If no token was generated, return the existing value for the $user
221+
*/
222+
if ( empty( $token ) ) {
223+
224+
/**
225+
* Return the user that was passed in to the filter
226+
*/
227+
return $user;
228+
229+
/**
230+
* If there is a token
231+
*/
232+
} else {
233+
234+
/**
235+
* Get the current user from the token
236+
*/
237+
$user = ! empty( $token ) && ! empty( $token->data->user->id ) ? $token->data->user->id : $user;
238+
239+
240+
}
241+
242+
243+
/**
244+
* Everything is ok, return the user ID stored in the token
245+
*/
246+
return absint( $user );
247+
} );

0 commit comments

Comments
 (0)