• Hi

    I am probably missing something obvious by how to I get the user data of an authenticated user in a custom endpoint?

    function get_user_data( $response ) {
    
    	// HOW DO I OUTPUT USER DATA HERE?
    	error_log(print_r($response, true));
     
      	return 'User';
    }
    
    add_action( 'rest_api_init', function () {
      register_rest_route( 'access/v1', '/content/(?P<id>\d+)', array(
        'methods' => 'GET',
        'callback' => 'get_user_data',
        'args' => array(
          'id' => array(
            'validate_callback' => function($param, $request, $key) {
              return is_numeric( $param );
            }
          ),
        ),
      ) );
    } );

    It is validating successful and I am getting the response but I am not sure how to parse the token to get the user data?

    I know with another plugin you do this.

    $user = JWTAuth::parseToken()->authenticate()
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter yasminwalsh

    (@yasminwalsh)

    function api_check_access(){
    
    	wp_send_json(array(
            'status' => true,
            'message' => __('', 'language'),
            'data' => wp_get_current_user()
        ));
    
    }
    
    add_action( 'rest_api_init', function () {
    
    	add_filter( 'jwt_auth_whitelist', function ( $endpoints ) {
    		return array(
    			'/wp-json/jwt-auth/v1/access/*'
    		);
    	});
    
    	register_rest_route('/jwt-auth/v1', 'access', array(
    		'methods' => 'POST',
    		'callback' => 'api_check_access',
    		'permission_callback' => function($request){	  
    		  	return is_user_logged_in();
    		}
    	)); 
    
    });
    Plugin Author Bagus

    (@contactjavas)

    Hi @yasminwalsh ,

    To get logged-in user data, you’ll need to pass a valid bearer token via authorization header to the request. Because the user will be determined using the token.

    Regards,
    Bagus

    Plugin Author Bagus

    (@contactjavas)

    Hi @yasminwalsh ,

    To get the logged-in user data, you’ll need to pass a valid bearer token via authorization header to the request. Because the user will be determined using the token.

    Regards,
    Bagus

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Output user data from custom endpoint’ is closed to new replies.