src/Controller/SecurityController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Role;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends BaseController
  14. {
  15.     /**
  16.      * @Route("/login", name="login")
  17.      * @param AuthenticationUtils $authenticationUtils
  18.      * @return Response
  19.      */
  20.     public function login(AuthenticationUtils $authenticationUtils): Response
  21.     {
  22.         // get the login error if there is one
  23.         $error $authenticationUtils->getLastAuthenticationError();
  24.         // last username entered by the user
  25.         $lastUsername $authenticationUtils->getLastUsername();
  26.         return $this->render('security/login.html.twig', [
  27.             'last_username' => $lastUsername,
  28.             'error' => $error,
  29.             ]);
  30.     }
  31.     /**
  32.      * @Route("/register/{email}/{password}/{name}", name="register")
  33.      *
  34.      * @param string $email
  35.      * @param string $password
  36.      * @param string $name
  37.      * @param UserPasswordEncoderInterface $passwordEncoder
  38.      * @return RedirectResponse
  39.      */
  40.     public function register(
  41.         string $email,
  42.         string $password,
  43.         string $name,
  44.         UserPasswordEncoderInterface $passwordEncoder,
  45.         EntityManagerInterface $em
  46.     ): RedirectResponse {
  47.         $user = (new User());
  48.         $user->setEmail($email)
  49.             ->setName($name)
  50.             ->setPassword($passwordEncoder->encodePassword($user$password))
  51.             ->setStatus(User::STATUS_ENABLED)
  52.             ->setToken(md5(uniqid(''true)))
  53.             ->addRole($em->getReference(Role::class, Role::ROLE_ID_USER));
  54.         $em->persist($user);
  55.         $em->flush();
  56.         return $this->redirectToRoute('homepage');
  57.     }
  58. }