FriendsOfSymfony / FOSUserBundle

Provides user management for your Symfony project. Compatible with Doctrine ORM & ODM, and custom storages.

Home Page:https://symfony.com/doc/master/bundles/FOSUserBundle/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SECURI Authenticator does not support the request. Please help

MatthieuMonray98 opened this issue · comments

Hi, here my Symfony code :

SecurityController.php

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\LoginType;
use App\Form\RegistrationType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;

class SecurityController extends AbstractController
{
    /**
     * @Route("/inscription", name="security_registration")
     */
    public function registration(Request $request, EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordHasher): Response
    {
        $user = new User();
        $form = $this->createForm(RegistrationType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // Hash user apassword before storing in database
            $hashedPassword = $passwordHasher->hashPassword($user, $user->getPassword());
            $user->setPassword($hashedPassword);

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

            return $this->redirectToRoute('security_login');
        }

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

    /**
     * @Route("/connexion", name="security_login")
     */
    public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
    {
        // Get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();

        // Last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        $form = $this->createForm(LoginType::class, [
            'email' => $lastUsername,
        ]);

        return $this->render('security/login.html.twig', [
            'form' => $form->createView(),
            'error' => $error,
        ]);
    }

    /**
     * @Route("/deconnexion", name="security_logout")
     */
    public function logout(Request $request, EventDispatcherInterface $eventDispatcher)
    {
        $user = $this->getUser();
        $event = new LogoutEvent($request, $user);
        $eventDispatcher->dispatch($event);

        return $this->redirectToRoute('homepage');
    }
}

security.yaml :

security:
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
    providers:
        users_in_memory:
            memory: ~
        in_database:
            entity:
                class: App\Entity\User
                property: email
    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN, requires_channel: https }
        - { path: ^/profile, roles: ROLE_USER, requires_channel: https }
        - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: GET }
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            provider: in_database
            form_login:
                login_path: security_login
                check_path: security_login_check
            logout:
                path: security_logout
                target: home

login.html.twig :

{```
% extends 'base.html.twig' %}

{% block body %}
    {% if app.user %}
        <h1>Bienvenue {{ app.user.username }} !</h1>
        <p>Vous êtes connecté en tant que {{ app.user.username }}.</p>
    {% else %}
        <h1>Connexion</h1>
        <form action="{{ path('security_login') }}" method="post">
            <div class="form-group">
                <input placeholder="Adresse email ..." required name="_username" type="text" class="form-control">
            </div>
            <div class="form-group">
                <input placeholder="Mot de passe ..." required name="_password" type="password" class="form-control">
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-success">Connexion</button>
            </div>
        </form>
    {% endif %}
{% endblock %}
Here the error : [2023-02-25T14:49:58.601108+00:00] security.DEBUG: Checking support on authenticator. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
[2023-02-25T14:49:58.601148+00:00] security.DEBUG: Authenticator does not support the request. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
[2023-02-25T14:49:59.716198+00:00] request.INFO: Matched route "security_login". {"route":"security_login","route_parameters":{"_route":"security_login","_controller":"App\\Controller\\SecurityController::login"},"request_uri":"https://localhost:8000/connexion","method":"POST"} []
[2023-02-25T14:49:59.734315+00:00] security.DEBUG: Checking for authenticator support. {"firewall_name":"main","authenticators":1} []
[2023-02-25T14:49:59.734379+00:00] security.DEBUG: Checking support on authenticator. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
[2023-02-25T14:49:59.734426+00:00] security.DEBUG: Authenticator does not support the request. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []

I'm closing this as it has nothing to do with FOSUserBundle