Disable CSRF when registering by a post method a new user in FOSUserBundle

I’m looking to combine a post rest method and FOSUserBundle to my symfony 3 application to register new user, so the problem is that when i use to post a new user I got this error Bad Request with "The CSRF token is invalid. Please try to resubmit the form

here is the page that appears when i use to post a new user

This is the registration method :

class DefaultController extends BaseController
{

 /**
 * @Route("/register", name="registermethod")
 */
public function registerAction(Request $request)
{

    $usermane = $request->query->get('username');
    $password = $request->query->get('password');
    $email = $request->query->get('email');
    /** @var $formFactory FactoryInterface */
    $formFactory = $this->get('fos_user.registration.form.factory');
    /** @var $userManager UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');
    /** @var $dispatcher EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $user = $userManager->createUser();
    $user->setEnabled(true);

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }
    $form = $formFactory->createForm();
    $user->setUsername($usermane);
    $user->setPlainPassword($password);
    $user->setEmail($email);
    $form->setData($user);

    $form->handleRequest($request);

    $form->submit($request->request->all());

    if ($form->isSubmitted()) {
        if ($user->getUsername() != null) {

            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $userManager->updateUser($user);

            /*****************************************************
             * Add new functionality (e.g. log the registration) *
             *****************************************************/
            $this->container->get('logger')->info(
                sprintf("New user registration: %s", $user)
            );

            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }

        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);

        if (null !== $response = $event->getResponse()) {
            return $response;
        }
    }

    return $this->render('@FOSUser/Registration/register.html.twig', array(
        'form' => $form->createView(),
    ));
}

I’use to post to this method in my angular application so the link is like this http://localhost:8000/showing1?username=azaz&[email protected]&password=123456789

Thank you in advance.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *