2

Symfony 3.3.10, lexik/jwt-authentication-bundle": "~2.0" I have endpoint for registration /api/registrations where I create user and set it to DB and return json with object user. And I have endpoint for authentication /api/login_check where JWT return token with refresh token My question, how in registration action call JWT authentication handler and return token with refresh token instead user object ?

this is my security

        login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        form_login:
            check_path: /api/login_check
            require_previous_session: false
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure

    register:
        pattern:  ^/api/registrations
        stateless: true
        anonymous: true

and my action for reg

public function postRegistrationAction(Request $request)
{
    $em = $this->get('doctrine')->getManager();
    $encoder = $this->container->get('security.password_encoder');
    $logger = $this->container->get('logger');

    try {
        $auth = $this->get('app.auth');

        /** @var User $user */
        $user = $auth->validateEntites('request', User::class, ['registration']);
        $password = $request->request->get('_password');

        $user
            ->setPassword($encoder->encodePassword($user, $password));

        $em->persist($user);
        $em->flush();

        return $this->createSuccessResponse($user, ['profile'], true);
    } catch (ValidatorException $e) {
        $view = $this->view(['message' => $e->getErrorsMessage()], self::HTTP_STATUS_CODE_BAD_REQUEST);
        $logger->error($this->getMessagePrefix().'validate error: '.$e->getErrorsMessage());
    } catch (\Exception $e) {
        $view = $this->view((array) $e->getMessage(), self::HTTP_STATUS_CODE_BAD_REQUEST);
        $logger->error($this->getMessagePrefix().'error: '.$e->getMessage());
    }

    return $this->handleView($view);
}
shuba.ivan
  • 3,824
  • 8
  • 49
  • 121

1 Answers1

0

solved, I rewrite AuthenticationSuccessHandler and added local parameter to return event array, then configed server for that and called it in registration action

        login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        form_login:
            check_path: /api/login_check
            require_previous_session: false
            username_parameter: _email
            password_parameter: _password
            success_handler: custom
            failure_handler: lexik_jwt_authentication.handler.authentication_failure

config service

    custom:
    class: "%app.authentication_success_handler.class%"
    arguments:
        - "@lexik_jwt_authentication.jwt_manager"
        - "@event_dispatcher"
    tags:
        - { name: monolog.logger, channel: security }

event data contain token info

    public function handleAuthenticationSuccess(UserInterface $user, $jwt = null, $returnEvent = false)
{
    if (null === $jwt) {
        $jwt = $this->jwtManager->create($user);
    }

    $response = new JWTAuthenticationSuccessResponse($jwt);
    $event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response);

    $this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event);
    $response->setData($event->getData());

    if ($returnEvent) {
        return $event;
    }

    return $response;
}

and then

    public function postRegistrationAction(Request $request)
{
    $em = $this->get('doctrine')->getManager();
    $encoder = $this->container->get('security.password_encoder');
    $logger = $this->container->get('logger');

    try {
        $auth = $this->get('app.auth');

        /** @var User $user */
        $user = $auth->validateEntites('request', User::class, ['registration']);
        $password = $request->request->get('_password');

        $user
            ->setPassword($encoder->encodePassword($user, $password));
        $em->persist($user);
        $em->flush();
        $lexikJwtAuthentication = $this->get('custom');
        $event = $lexikJwtAuthentication->handleAuthenticationSuccess($user, null, true);

        return $this->createSuccessResponse($event->getData());

and user after registration got authorized and my action return token information

shuba.ivan
  • 3,824
  • 8
  • 49
  • 121