Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\TwoFactorProvider::requestAuthenticationCode PHP Method

requestAuthenticationCode() public method

Ask for email authentication code.
public requestAuthenticationCode ( Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface $context ) : Response | null
$context Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface
return Symfony\Component\HttpFoundation\Response | null
    public function requestAuthenticationCode(AuthenticationContextInterface $context)
    {
        $user = $context->getUser();
        $request = $context->getRequest();
        $session = $context->getSession();
        // Display and process form
        $authCode = $request->get($this->authCodeParameter);
        if ($authCode !== null) {
            if ($this->authenticator->checkCode($user, $authCode)) {
                $context->setAuthenticated(true);
                return new RedirectResponse($request->getUri());
            }
            $session->getFlashBag()->set('two_factor', 'scheb_two_factor.code_invalid');
        }
        // Force authentication code dialog
        return $this->renderer->render($context);
    }

Usage Example

 /**
  * @test
  */
 public function requestAuthenticationCode_validCode_returnRedirect()
 {
     $request = $this->getPostCodeRequest();
     $context = $this->getAuthenticationContext(null, $request);
     $this->stubAuthCodeManager(true);
     $returnValue = $this->provider->requestAuthenticationCode($context);
     $this->assertInstanceOf("Symfony\\Component\\HttpFoundation\\RedirectResponse", $returnValue);
     $this->assertEquals("/some/path", $returnValue->getTargetUrl());
 }