I’m getting the following error: “Could not find a PSR-18 compatible HTTP client. Please install one, or provide one using the setHttpClient()
method.”
The code is trying to update the metadata in Auth0 after it’s updated in WordPress. Here is the code:
use Auth0\SDK\Auth0;
use Auth0\SDK\Configuration\SdkConfiguration;
use Auth0\SDK\API\Authentication;
use Auth0\SDK\API\Management;
use Auth0\SDK\Utility\HttpResponse;
use GuzzleHttp\Client;
function update_auth0_metadata_with_wp_roles($user_id) {
// Get the WordPress user object
$user = get_userdata($user_id);
// Get the user's roles
$roles = $user->roles;
// Get the user's Auth0 user_id
$all_meta_for_user = get_user_meta($user_id);
$auth0_user_id = $all_meta_for_user['wp_auth0_id'][0];
// If we don't have an Auth0 user_id, we can't update the metadata
if (empty($auth0_user_id)) {
write_log(get_user_meta($user_id));
return;
}
// Initialize the Auth0 SDK
$auth0_domain = WP_Auth0_Options::Instance()->get('domain');
$client_id = WP_Auth0_Options::Instance()->get('client_id');
$client_secret = WP_Auth0_Options::Instance()->get('client_secret');
$config = [
'domain' => $auth0_domain,
'clientId' => $client_id,
'clientSecret' => $client_secret,
'audience' => ['https://' . $auth0_domain .'/api/v2/'] ,
'tokenAlgorithm' => 'RS256',
'tokenEndpoint' => 'https://' . $auth0_domain . '/oauth/token',
'debug' => true
];
try {
//$auth0 = new \Auth0\SDK\Auth0($config);
$auth0 = new Auth0($config);
// Get an access token
$token = $auth0->getAccessToken();
// Initialize the Management API client
//$management = $auth0->management();
// Set the headers for the Management API client
$management->getHttpClient()->getConfig()->setHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
]);
// Update the user's metadata
$management->users()->update($auth0_user_id, [
'user_metadata' => ['wp_roles' => $roles]
]);
} catch (Exception $e) {
// Handle any errors
error_log('Error updating Auth0 metadata: ' . $e->getMessage() . "\n" . $e->getTraceAsString());
error_log('Auth0 config: ' . print_r($config, true));
}
}
// Hook our function to run when user roles are updated
add_action('set_user_role', 'update_auth0_metadata_with_wp_roles', 10, 1);
//add_action('add_user_role', 'update_auth0_metadata_with_wp_roles', 10, 1);
add_action('remove_user_role', 'update_auth0_metadata_with_wp_roles', 10, 1);