<?php
namespace App\Controller;
use App\Entity\Role;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends BaseController
{
/**
* @Route("/login", name="login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/register/{email}/{password}/{name}", name="register")
*
* @param string $email
* @param string $password
* @param string $name
* @param UserPasswordEncoderInterface $passwordEncoder
* @return RedirectResponse
*/
public function register(
string $email,
string $password,
string $name,
UserPasswordEncoderInterface $passwordEncoder,
EntityManagerInterface $em
): RedirectResponse {
$user = (new User());
$user->setEmail($email)
->setName($name)
->setPassword($passwordEncoder->encodePassword($user, $password))
->setStatus(User::STATUS_ENABLED)
->setToken(md5(uniqid('', true)))
->addRole($em->getReference(Role::class, Role::ROLE_ID_USER));
$em->persist($user);
$em->flush();
return $this->redirectToRoute('homepage');
}
}