Selamlar, bir cok defa tekrar tekrar yaptigim ancak ihtiyacim oldugunda bulamadimdan ornek olmasi acisindan kullaniciyi elle fonksiyonel sekilde nasil login edeceginizin bir ornegini buraya ekliyorum. Bu ayrica unit testlerdede login islemi yapmanizi saglar.
Ornekte kullanicinin uyeligi onaylaniyor ve elimizdeki user objesi ile kullanici login ederek yonlendiriyoruz.
Burada security firewall'ununuzun adinin main oldugunu varsayiyoruz.
/**
* @Route("/account-confirmation/{confirmation_token}", requirements={"confirmation_token" = "[a-z0-9]{32}"}, name="security_account_confirmation")
*/
public function accountConfirmationAction(Request $request, $confirmation_token)
{
$user = $this->getDoctrine()
->getRepository('AppBundle:User')
->findOneBy(['confirmationToken' => $confirmation_token]);
if (!$user) {
throw $this->createNotFoundException();
}
if ($user->getIsVerified()) {
throw $this->createNotFoundException();
}
$user->setConfirmationToken(null);
$user->setIsVerified(true);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->get('event_dispatcher')->dispatch(SecurityEvents::INTERACTIVE_LOGIN, $event);
return $this->redirectToRoute('dashboard');
}