Symfony 7 验证用户电子邮件功能的问题

问题描述 投票:0回答:1

早上好。 我正在 Symfony 7 上进行训练(到目前为止没有太多问题),但现在我遇到了问题。 欲了解更多信息: PHP:8.2.13Composer 2.7.1,Windows 11 64(我们永远不知道^^)

当用户想要注册时,他会收到一封电子邮件地址确认电子邮件,当他点击该电子邮件时,他会被重定向到“/login”页面,但用户的电子邮件地址并未经过验证。 所以他无法连接。 用户在数据库中。 我没有任何错误消息。

用户收到的验证链接的形式为: http://localhost:8000/verify/email?expires=1711156044&signature=nn5%2BIfAw8bzly1yrXpSDDC92QZY61n0n8gGojDFgXJQ%3D&token=Sgpw23MGVEqVclZxG%2FV2OX%2FOrWq6jo20m95eRtu%2BeAU%3D

我给你我的RegistrationController的代码 我想问题来自 **verifyUserEmail ** 方法

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Security\AppAuthenticator;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;

class RegistrationController extends AbstractController
{
    private EmailVerifier $emailVerifier;

    public function __construct(EmailVerifier $emailVerifier, LoggerInterface $logger)
    {
        $this->emailVerifier = $emailVerifier;
        $this->logger =$logger;
    }

    #[Route('/register', name: 'app_register')]
    public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
    {
        $user = new User();
        $form = $this->createForm(RegistrationFormType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $user->setPassword(
                $userPasswordHasher->hashPassword(
                    $user,
                    $form->get('plainPassword')->getData()
                )
            );

            $entityManager->persist($user);
            $entityManager->flush();

            $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
                (new TemplatedEmail())
                    ->from(new Address('[email protected]', 'Flow'))
                    ->to($user->getEmail())
                    ->subject('Confirmez votre e-mail')
                    ->htmlTemplate('registration/confirmation_email.html.twig')
            );;
            return $this->redirectToRoute('home.index');
        }

        return $this->render('registration/register.html.twig',[
            'registrationForm' => $form->createView()
        ]);

    }

    #[Route('/verify/email', name: 'app_verify_email')]
    public function verifyUserEmail(Request $request, TranslatorInterface $translator, EntityManagerInterface $entityManager): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

        $user = $this->getUser();
        $token = $request->get('token');

        if ($user && $this->isCsrfTokenValid('verify_email', $token)) {
            try {
                $this->emailVerifier->handleEmailConfirmation($request, $user);
                return $this->redirectToRoute('app_login');
            } catch (VerifyEmailExceptionInterface $exception) {
                $this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
            }
        } else {
            $this->addFlash('verify_email_error', 'Les données de vérification d\'e-mail ne sont pas valides.');
        }
        return $this->redirectToRoute('app_login');
    }

}

我不知道它可以治愈,但我的composer.json:

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.2",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/dbal": "^3",
        "doctrine/doctrine-bundle": "^2.12",
        "doctrine/doctrine-migrations-bundle": "^3.3",
        "doctrine/orm": "^3.1",
        "phpdocumentor/reflection-docblock": "^5.3",
        "phpstan/phpdoc-parser": "^1.26",
        "symfony/asset": "7.0.*",
        "symfony/asset-mapper": "7.0.*",
        "symfony/console": "7.0.*",
        "symfony/doctrine-messenger": "7.0.*",
        "symfony/dotenv": "7.0.*",
        "symfony/expression-language": "7.0.*",
        "symfony/flex": "^2",
        "symfony/form": "7.0.*",
        "symfony/framework-bundle": "7.0.*",
        "symfony/http-client": "7.0.*",
        "symfony/intl": "7.0.*",
        "symfony/mailer": "7.0.*",
        "symfony/mime": "7.0.*",
        "symfony/monolog-bundle": "^3.0",
        "symfony/notifier": "7.0.*",
        "symfony/process": "7.0.*",
        "symfony/property-access": "7.0.*",
        "symfony/property-info": "7.0.*",
        "symfony/runtime": "7.0.*",
        "symfony/security-bundle": "7.0.*",
        "symfony/serializer": "7.0.*",
        "symfony/stimulus-bundle": "^2.16",
        "symfony/string": "7.0.*",
        "symfony/translation": "7.0.*",
        "symfony/twig-bundle": "7.0.*",
        "symfony/validator": "7.0.*",
        "symfony/web-link": "7.0.*",
        "symfony/yaml": "7.0.*",
        "symfonycasts/verify-email-bundle": "^1.17",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0",
        "vich/uploader-bundle": "^2.3"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php73": "*",
        "symfony/polyfill-php74": "*",
        "symfony/polyfill-php80": "*",
        "symfony/polyfill-php81": "*",
        "symfony/polyfill-php82": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd",
            "importmap:install": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "7.0.*"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^9.5",
        "symfony/browser-kit": "7.0.*",
        "symfony/css-selector": "7.0.*",
        "symfony/debug-bundle": "7.0.*",
        "symfony/maker-bundle": "^1.0",
        "symfony/phpunit-bridge": "^7.0",
        "symfony/stopwatch": "7.0.*",
        "symfony/web-profiler-bundle": "7.0.*"
    }
}

我使用 MailDev 发送电子邮件

预先感谢您的帮助。

php symfony
1个回答
0
投票

我修改了代码,但结果是一样的:(

<?php
public function verifyUserEmail(Request $request, TranslatorInterface $translator, EntityManagerInterface $entityManager): Response
{
    $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
    try {
        $this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
    }catch (VerifyEmailExceptionInterface $exception){
        $this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
        return $this->redirectToRoute('app_register');
    }
    $this->addFlash('success', 'Your email has been verified.');
    return $this->redirectToRoute('app_register');
}
© www.soinside.com 2019 - 2024. All rights reserved.