src/Controller/SecurityController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ChangePasswordFormType;
  5. use Exception;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. #[Route(name'app_')]
  13. class SecurityController extends AbstractController
  14. {
  15.     #[Route('/login'name'login')]
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         $error $authenticationUtils->getLastAuthenticationError();
  19.         $lastUsername $authenticationUtils->getLastUsername();
  20.         return $this->render('@EasyAdmin/page/login.html.twig', [
  21.             // parameters usually defined in Symfony login forms
  22.             'error' => $error,
  23.             'last_username' => $lastUsername,
  24.             // OPTIONAL parameters to customize the login form:
  25.             // the translation_domain to use (define this option only if you are
  26.             // rendering the login template in a regular Symfony controller; when
  27.             // rendering it from an EasyAdmin Dashboard this is automatically set to
  28.             // the same domain as the rest of the Dashboard)
  29.             'translation_domain' => 'admin',
  30.             // the title visible above the login form (define this option only if you are
  31.             // rendering the login template in a regular Symfony controller; when rendering
  32.             // it from an EasyAdmin Dashboard this is automatically set as the Dashboard title)
  33.             'page_title' => 'Rigs',
  34.             // the string used to generate the CSRF token. If you don't define
  35.             // this parameter, the login form won't include a CSRF token
  36.             'csrf_token_intention' => 'authenticate',
  37.             // the URL users are redirected to after the login (default: '/admin')
  38.             'target_path' => $this->generateUrl('homepage'),
  39.             // the label displayed for the username form field (the |trans filter is applied to it)
  40.             'username_label' => 'Email',
  41.             // the label displayed for the password form field (the |trans filter is applied to it)
  42.             'password_label' => 'HasÅ‚o',
  43.             // the label displayed for the Sign In form button (the |trans filter is applied to it)
  44.             'sign_in_label' => 'Log in',
  45.             // the 'name' HTML attribute of the <input> used for the username field (default: '_username')
  46.             'username_parameter' => '_username',
  47.             // the 'name' HTML attribute of the <input> used for the password field (default: '_password')
  48.             'password_parameter' => '_password',
  49.         ]);
  50.     }
  51.     #[Route('/logout'name'logout')]
  52.     public function logout(): void
  53.     {
  54.         // controller can be blank: it will never be executed!
  55.         throw new Exception('Don\'t forget to activate logout in security.yaml');
  56.     }
  57.     #[Route('/change-password'name'change_password')]
  58.     public function reset(Request $requestUserPasswordHasherInterface $passwordHasher): Response
  59.     {
  60.         /** @var User $user */
  61.         $user $this->getUser();
  62.         // The token is valid; allow the user to change their password.
  63.         $form $this->createForm(ChangePasswordFormType::class);
  64.         $form->handleRequest($request);
  65.         if ($form->isSubmitted() && $form->isValid()) {
  66.             // Encode the plain password, and set it.
  67.             $encodedPassword $passwordHasher->hashPassword(
  68.                 $user,
  69.                 $form->get('plainPassword')->getData()
  70.             );
  71.             $user->setPassword($encodedPassword);
  72.             $this->getDoctrine()->getManager()->flush();
  73.             $this->addFlash('success''Password changed successfully.');
  74.             return $this->redirectToRoute('app_change_password');
  75.         }
  76.         return $this->render('reset_password/reset.html.twig', [
  77.             'resetForm' => $form->createView(),
  78.         ]);
  79.     }
  80. }