Bonjour à tous j'ai crée un login avec symfony 4, hier tous marcher bien, mai smaintenant ça ne marche pas et je n'ai même pas d'erreur pour voir d'ou est ce que ça viens :(
quelqu'un aurai déjà eu ce problème ?

code login.html.twig

{% extends 'base_admin.html.twig' %}

{% block stylesheets %}
    {{ encore_entry_link_tags('login') }}
{% endblock %}

{% block body %}
    <div class="container-fluid login-page">
        <div class="container">
            <div class="row">
                <div class="col-md-4"></div>
                <div class="col-md-4" style="margin-top: 8cm">
                    <form method="post">
                        {% if error %}
                            <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
                        {% endif %}
                        <div class="card">
                            <div class="card-header text-center">
                                <h3 class="text-white">{{ 'label.login'|trans }}</h3>
                            </div>
                            <div class="card-body">
                                <div class="input-group form-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text text-white"><i class="far fa-user"></i></span>
                                    </div>
                                    <input type="email" name="email" class="form-control" placeholder="{{ 'label_identifiant'|trans }}" value="{{ last_username }}" required autofocus>

                                </div>
                                <div class="input-group form-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text text-white"><i class="fas fa-key"></i></span>
                                    </div>
                                    <input name="password" type="password" class="form-control" placeholder="{{ 'label_password'|trans }}" required>
                                </div>
                                <div class="row align-items-center remember">
                                    <input type="checkbox" name="_remember_me">Remember Me
                                </div>
                                <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
                                <div class="form-group">
                                    <input type="submit" value="{{ 'label.login'|trans }}" class="btn btn-info float-right">
                                </div>
                            </div>
                            <div class="card-footer">
                                <div class="d-flex justify-content-center">
                                    <a href="#" class="text-white">{{ 'forgot_password'|trans }}</a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="col-md-4"></div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javascript %}
{{ encore_entry_script_tags('login') }}
{% endblock %}

Security controller :

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends AbstractController
{
    /**
     * @Route("/login", name="app_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('admin/security/login.html.twig', [
            'last_username' => $lastUsername,
            'error' => $error,
        ]);
    }
}

Login Form Authenticator

<?php

namespace App\Security;

use App\Entity\Users;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
 * Class LoginFormAuthenticator
 *
 * @package App\Security
 *
 */
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    private $entityManager;

    private $router;

    private $csrfTokenManager;

    private $passwordEncoder;

    private $authorizationChecker;

    /**
     * LoginFormAuthenticator constructor.
     *
     * @param EntityManagerInterface        $entityManager
     * @param RouterInterface               $router
     * @param CsrfTokenManagerInterface     $csrfTokenManager
     * @param UserPasswordEncoderInterface  $passwordEncoder
     * @param AuthorizationCheckerInterface $authorizationChecker
     */
    public function __construct(
        EntityManagerInterface $entityManager,
        RouterInterface $router,
        CsrfTokenManagerInterface $csrfTokenManager,
        UserPasswordEncoderInterface $passwordEncoder,
        AuthorizationCheckerInterface $authorizationChecker
    ) {
        $this->entityManager = $entityManager;
        $this->router = $router;
        $this->csrfTokenManager = $csrfTokenManager;
        $this->passwordEncoder = $passwordEncoder;
        $this->authorizationChecker = $authorizationChecker;
    }

    /**
     * {@inheritdoc}
     */
    public function supports(Request $request)
    {
        return 'app_login' === $request->attributes->get('_route')
               && $request->isMethod('POST');
    }

    /**
     * {@inheritdoc}
     */
    public function getCredentials(Request $request)
    {
        $credentials = [
            'email'      => strtolower($request->request->get('email')),
            'password'   => $request->request->get('password'),
            'csrf_token' => $request->request->get('_csrf_token'),
        ];
        $request->getSession()->set(
            Security::LAST_USERNAME,
            $credentials['email']
        );

        return $credentials;
    }

    /**
     * {@inheritdoc}
     */
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $token = new CsrfToken('authenticate', $credentials['csrf_token']);
        if (!$this->csrfTokenManager->isTokenValid($token)) {
            throw new InvalidCsrfTokenException();
        }

        $user = $this->entityManager->getRepository(Users::class)->findOneBy(['email' => $credentials['email']]);

        if (!$user) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('error.invalid_user_account');
        }

        // user disabled
        if (!$user->isEnabled()) {
            throw new CustomUserMessageAuthenticationException('error.account_disabled');
        }

        return $user;
    }

    /**
     * {@inheritdoc}
     */
    public function checkCredentials($credentials, UserInterface $user)
    {
        return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
    }

    /**
     * {@inheritdoc}
     */
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
            return new RedirectResponse($targetPath);
        }

        $urlName = $this->authorizationChecker->isGranted(Users::ROLE_ADMIN) ? 'admin_index' : 'index_front';
//            : ($this->authorizationChecker->isGranted(Users::ROLE_CHEF_RESEAUX) ? 'app_login' : '');

        return new RedirectResponse($this->router->generate($urlName));
    }

    /**
     * {@inheritdoc}
     */
    protected function getLoginUrl()
    {
        return $this->router->generate('app_login');
    }
}

mon fichier .env est bien configurer vu que je peux executer mes fixtures

1 réponse


CedLP
Auteur
Réponse acceptée

Bon j'ai pu résoudre mon problème, mon docker a été mal configurer du coup mon appli n'avais pas accés à la base de données.
Merci