3
votes

Hi I'm using API Platform. An API framework based on symfony.

I have created Entities and exposed them with the @ApiResource annotation, so I can see them in the API doc for CRUD.

I would like to add a custom Controller in the swagger api doc. For example my authentification Controller.

How can I do that?

My TokenController:

class TokenController extends Controller
{
    /**
     * @Route(path="/auth", name="auth")
     *
     * @param Request $request
     * @return JsonResponse
     * @throws \Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException
     */
    public function tokenAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository(User::class)->findOneBy(array(
            'username' => $request->getUser(),
        ));

        if(!$user){
            throw $this->createNotFoundException('No user');
        }

        $isValid = $this->get('security.password_encoder')->isPasswordValid($user, $request->getPassword());

        if(!$isValid){
            throw new BadCredentialsException();
        }

        $token = $this->get('lexik_jwt_authentication.encoder')->encode(array(
            'id' => $user->getId(),
            'username' => $user->getUsername(),
            'roles' => $user->getRoles(),
        ));

        return new JsonResponse(array(
            'token' => $token
        ));
    }
}
1

1 Answers

0
votes

You have to create a Swagger Decorator https://api-platform.com/docs/core/swagger#header and add your custom swagger configuration for your endpoint.