phpsymfonysymfony5

scheb/2fa not detected/working on my project


I wish to add two-factor authentication to my project and I came across your solution which seems great to me. Initially, the installation seemed straightforward, but I must be missing something because my authentication still works with email and password as usual.

I follow the installation guide and this is a part of my security.yaml:

main:
    lazy: true
    provider: app_user_provider
    remember_me:
        secret:   '%kernel.secret%'
        token_provider: 'Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider'
        lifetime: 68400
    form_login:
        # "login" is the name of the login route
        login_path: login
        check_path: login
        enable_csrf: true
    logout:
        path: logout
        # where to redirect after logout
        target: login
    two_factor:
        auth_form_path: 2fa_login    # The route name you have used in the routes.yaml
        check_path: 2fa_login_check  # The route name you have used in the routes.yaml

My SecurityController look like this :

#[Route('/login', name: 'login')]
public function index(AuthenticationUtils $authenticationUtils): Response
{
    if($this->isGranted('ROLE1') || $this->isGranted('ROLE2') ){
        return $this->redirectToRoute('home_master_user');
    }
   elseif($this->isGranted('ROLE3') && $this->isGranted('ROLE')) {
        return $this->redirectToRoute('home');
    }
    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();
    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('login/index.html.twig', [
        'last_username' => $lastUsername,
        'error' => $error
    ]);
}

To be honest, I don't understand why it's not working even after following the various tutorials I've read.


Solution

  • After a lot of search it was just a problem in the security.yaml :

    main:
        two_factor:
            auth_form_path: 2fa_login    # The route name you have used in the routes.yaml
            check_path: 2fa_login_check  # The route name you have used in the routes.yaml
            provider: app_user_provider
            enable_csrf: true
    

    I put the provider in two factor and now it works