I would like to work with the management part of the sdk.
But i can’t seem to find any docs regarding the symfony sdk.
I looked add the source code but i can’t seem to find any service a can inject in my services.
what is the best approach to do this, are there any examples?
I’m looking to manage the users from within my symfony project.
Hi @geert.huygen Thanks for your question. That’s a great point — we should do better at explaining how to use the Management APIs from that SDK.
At present, we don’t expose this API through a Symfony service; nevertheless, it can be used by directly calling the underlying Auth0-PHP SDK instance already instantiated and configured by your Symfony SDK instance.
Here’s an example of accessing Management API endpoints in this manner:
Note: I’m writing this from memory as I’m not at my machine at the moment, so this probably won’t work exactly as-is and may require minor adjustments.
<?php
declare(strict_types=1);
namespace App\Example\Controllers;
use Auth0\SDK\Auth0;
use Auth0\Symfony\Security\Authenticator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
final class ExampleController extends AbstractController
{
public function __construct(
private Authenticator $authenticator,
private RouterInterface $router,
) {
}
public function updateUserColorMetadata(Request $request): Response
{
$user = $this->getUser();
$endpoint = $this->getManagement()->users();
$colors = ['red', 'blue', 'green', 'black', 'white', 'yellow', 'purple', 'orange', 'pink', 'brown'];
// Update the user's metadata with the Management API.
$endpoint->update(
id: $user()->getId(),
body: [
'user_metadata' => [
'color' => $colors[random_int(0, count($colors) - 1)]
]
]
);
$metadata = $endpoint->get($user->getId()); // Retrieve the user's updated metadata.
$metadata = json_decode($metadata->getBody(), true); // JSON -> PHP array.
$color = $metadata['user_metadata']['color'] ?? 'unknown';
$name = $user->getName();
return new Response("Hello {$name}! Your favorite color is now {$color}.");
}
private function getManagement()
{
return $this->getSdk()->management();
}
private function getSdk(): Auth0
{
return $this->authenticator->service->getSdk();
}
}
Unfortunately, every service is private and not available for dependency injection.
The Authenticator has no function or prop called service
I believe you meant the Authorizer class.
This class has a getService methode.
However, this service is not accessible. Here’s the output from the Symfony CLI debug:container:
Information for Service "auth0.authorizer"
==========================================
---------------- -------------------------------------
Option Value
---------------- -------------------------------------
Service ID auth0.authorizer
Class Auth0\Symfony\Security\Authorizer
Tags -
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Usages security.command.debug_firewall
security.authenticator.manager.main
.service_locator.6OJrPuT
---------------- -------------------------------------
! [NOTE] The "auth0.authorizer" service or alias has been removed or inlined when the container was compiled.
I think I’m just going to use the Auth0-PHP SDK package and wrap this with a service of my own.
However, this service is not accessible. Here’s the output from the Symfony CLI debug:container:
It sounds like your service instance may not have been initialized before it was called. In your app’s case, the framework firewall may not have booted up the service before you attempted to call it. You can define a service to always be available through your application’s services.yaml file.
I think I’m just going to use the Auth0-PHP SDK package and wrap this with a service of my own.