How to set cookie information within a Symfony2 controller

Asked

Viewed 23 times

1

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

inserir a descrição da imagem aqui

I have the following controller:

<?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 authenticated methodUsuario is the following:

	

<?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 $this->getUsuarioManager()->authenticatUsuario($user) method is equal to the above.

Could someone give me a light on how to set this information and return to $Answer in controller?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.