I have a requirement that a certain API endpoint returns the currently authenticated user, so I'm trying to add a custom operation action to my User entity under the route /auth/me.
If I add the @route() annotation to my action with the path /auth/me, api-platform complains that the identifier is invalid.
If I add the @route() annotation to my action with the path /auth/{id} and try to intercept the ID when it is me, api-platform still complains because it cannot find a User with the ID of me before it runs the action.
How do I add a custom operation action where I can use an injected service to return an instance of User? In this case I would be using the symfony security component and returning the currently authenticated user.
namespace AppBundle\Action;
use AppBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class AuthenticatedUser
{
/**
* @Route(
* name="api_users_get_authenticated",
* path="/auth/me",
* defaults={"_api_resource_class"=User::class, "_api_item_operation_name"="get-authenticated"}
* )
* @Method("GET")
*/
public function __invoke($data)
{
// Get currently authenticated user from symfony security component here
}
}
I have a requirement that a certain API endpoint returns the currently authenticated user, so I'm trying to add a custom operation action to my
Userentity under the route/auth/me.If I add the @route() annotation to my action with the path
/auth/me, api-platform complains that the identifier is invalid.If I add the @route() annotation to my action with the path
/auth/{id}and try to intercept the ID when it isme, api-platform still complains because it cannot find aUserwith the ID ofmebefore it runs the action.How do I add a custom operation action where I can use an injected service to return an instance of
User? In this case I would be using the symfony security component and returning the currently authenticated user.