Skip to content

Instantly share code, notes, and snippets.

@arcanisgk
Created December 22, 2021 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcanisgk/1bd8ad91a924492bfb6b7a4515040f46 to your computer and use it in GitHub Desktop.
Save arcanisgk/1bd8ad91a924492bfb6b7a4515040f46 to your computer and use it in GitHub Desktop.
activation of user account
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\AccountActivationType;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
class AccountActivationController extends AbstractController
{
/**
* @Route("/account_activation", name="account_activation")
*/
public function index(
Request $request,
ManagerRegistry $doctrine,
UserPasswordHasherInterface $passwordHasher
): Response {
$form = $this->CreateForm(AccountActivationType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form['email']->getData();
$plainPassword = $form['code']->getData();
$em = $doctrine->getManager();
$userObj = $doctrine->getRepository(User::class)->findOneBy(['email' => $email]);
$activation_hash = $userObj->getActivationHash();
$user = new User();
$user->setPassword($activation_hash);
if ($passwordHasher->isPasswordValid($user, $plainPassword)) {
$userObj->setActivated(true);
$userObj->setActivationHash('');
$em->persist($userObj);
$em->flush();
return $this->redirectToRoute('login');
} else {
//add error flash
}
}
return $this->render('account_activation/index.html.twig', [
'account_activation_form' => $form->createView(),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment