Voici votre URL de partage https://sharemycode.io/c/c799a61 (Cliquer pour copier) (Copié)

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use App\Form\ResetPasswordRequestFormType;
use App\Repository\UserRepository;
use App\Service\SendMailService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
controller

class SecurityController extends AbstractController
{
    #[Route(path: '/login', name: 'app_login')]

    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        // if ($this->getUser()) {
        //     return $this->redirectToRoute('target_path');
        // }

        // 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(path: '/logout', name: 'app_logout')]
    public function logout(): void
    {
        throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
    }

    #[Route(path: '/oubli-pass', name: 'forgotten_password')]
    public function forgottenPassword(
        Request $request,
        UserRepository $userRepository,
        TokenGeneratorInterface $tokenGenerator,
        EntityManagerInterface $entityManager,
        SendMailService $mail
    ): Response {
        $form =  $this->createForm(ResetPasswordRequestFormType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            //on cherche l'user par son email
            $user = $userRepository->findOneByEmail($form->get('email')->getData());
            //on verifie si on a un utilisateur
            if ($user) {
                //on genere un token de renitialisation
                $token = $tokenGenerator->generateToken();
                $user->setResetToken($token);
                $entityManager->persist($user);
                $entityManager->flush();
                //on genere un lien de renitialisation du mot de passe
                $url = $this->generateUrl(
                    'reset_pass',
                    ['token' => $token],
                    UrlGeneratorInterface::ABSOLUTE_URL
                );

                // on cree les donnees du mail

                $context = compact('url', 'user');

                //envoi du mail
                $mail->send(
                    'no-reply@ecom.fr',
                    $user->getEmail(),
                    'reinitialisation de mot de passe',
                    'password_reset',
                    $context
                );
                $this->addFlash('sucess', 'Email envoyé avec succes');
                return $this->redirectToRoute('app_login');
            }
            $this->addFlash('success', 'un probleme est survenu');
            return $this->redirectToRoute('app_login');
        }
        return $this->render('security/reset_password_request.html.twig', [
            'requestPassForm' => $form->createView()
        ]);
    }


    #[Route(path: '/oubli-pass/{token}', name:'reset_pass')]
    public function resetPass(
        Request $request,
        UsersRepository $usersRepository,
        EntityManagerInterface $entityManager,
        UserPasswordHasherInterface $passwordHasher,
        string $token
    ): Response{
        //on verifie si on a ce token dans la bdd
        $user = $usersRepository->findOneByResetToken($token);
        dd($user);
    }
}

Informations

Cet extrait a été créé le 23 sept. 2022 à 00:15:57

Cet extrait expire le 23 oct. 2022 à 00:15:57

Langage : php

Logo php

Link

Voici votre URL de partage : https://sharemycode.io/c/c799a61 Copié

Demander la suppression