How to set information in the cookie within a Symfony2 controller

1

I need to set some information in the cookie when a new user creates their registration in the system. The registration form is this:

Ihavethefollowingcontroller:

<?php
/**
 * Exibe o convite para usuários anônimos.
 * @Route("/aceitar", name="convite_aceitar")
 * @Method({"GET", "POST"})
 * @Template("ClienteBundle:/Convite:cadastrar.html.twig")
 * @ParamConverter("convite", options={"mapping": {"token": "token"}})
 */
public

function exibirConviteParaAnonimoAction(Request $request, UserConvite $convite)
{
if ($request->getMethod() == 'POST') {
$formCadastro->handleRequest($request);
if ($formCadastro->isValid()) {
	try {
		$user = $formCadastro->getData();
		$userConvidado = $manager->aceitarConvite($convite, $user);
		$manager->autenticaUsuario($userConvidado);

 $this->get('session')->getFlashBag()->add('success', 'Convite aceito com sucesso.');
	return $this->redirect($this->generateUrl('contrato_list', array()));
}
   catch(Exception $e) {
				$msg = "Oops! Ocorreu um erro. <br />" . $e->getMessage();
				$this->get('session')->getFlashBag()->set('error', $msg);
			}
		}
	}

	$params['form_cadastro'] = $formCadastro->createView();
	return $params;
}

The authenticaUsuario method is as follows:

	

<?php
public

function autenticaUsuario(Usuario $usuario)
{
 $providerKey = 'usuario_conta'; // your firewall name
 $token = new UsernamePasswordToken($usuario, null, $providerKey, $usuario->getRoles());
 $this->securityContext->setToken($token);
 return $token;
}

OcookiequequerosetaréumJWTeometodoestálocalizadoemAuxiliarManager::setJWTCookie()
/**
 *
 * Cria o JWT para o usuario em questão e salva no Cookie do navegador.
 * @param Request $request
 * @param UserInterface $user
 * @param Response $response
 */
public

function setJWTCookie(Request $request, Usuario $user, Response $response)
{
 $baseUrl = $this->getDominioBase($request->getHost());
 $jwt = $this->jwtManager->create($user);
 $cookieJWT = new Cookie('user_jwt', $jwt, 0, '/', '.' . $baseUrl, true, false);
 $response->headers->setCookie($cookieJWT);
 $this->getUsuarioManager()->autenticaUsuario($user);
 return $cookieJWT;
}

Note: The method $ this-> getUsuarioManager () -> authenticatesUsuario ($ user) is equal to the one above.

Can anyone give me a light on how to set this information and return the $ response on the controller?

    
asked by anonymous 01.11.2017 / 21:10

0 answers