Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
6 / 6
AuthenticationSuccessHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
6 / 6
 setJwtManager
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 onAuthenticationSuccess
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
<?php
/*
 * This file is part of the Incipio package.
 *
 * (c) Théo FIDRY <theo.fidry@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace FrontBundle\Security\Http\Authentication;
use FOS\UserBundle\Model\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
/**
 * Class with the default authentication success handling logic.
 *
 * @author Théo FIDRY <theo.fidry@gmail.com>
 */
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
{
    /**
     * @var JWTManager
     */
    private $jwtManager;
    /**
     * @param JWTManager $jwtManager
     */
    public function setJwtManager(JWTManager $jwtManager)
    {
        $this->jwtManager = $jwtManager;
    }
    /**
     * {@inheritdoc}
     *
     * Overrides event to add API token to the user's session.
     */
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        /*
         * @var UserInterface Note this will return a ApiBundle\Entity\User instance; this dependence to ApiBundle
         *                    is due to the fact that this is the user class defined at the application
         *                    configuration level worry here.
         */
        $user = $token->getUser();
        $apiToken = $this->jwtManager->create($user);
        $request->getSession()->set('api_token', $apiToken);
        return parent::onAuthenticationSuccess($request, $token);
    }
}