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+
2326if ( ! defined ( 'WPINC ' ) ) {
2427 die;
2528}
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