I am working on an API in symfony that uses JWT for authentication. For JWT i use LexikJWTAuthenticationBundle and for token refreshment i use JWTRefreshTokenBundle. What i want to do is to authenticate user via token and give it refresh token. In security i have:
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/api/login_check
stateless: true
anonymous: true
form_login:
check_path: fos_user_security_check
username_parameter: username
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
register:
pattern: ^/api/register
anonymous: true
stateless: true
refresh:
pattern: ^/api/token/refresh
stateless: true
anonymous: true
api:
pattern: ^/api
provider: fos_userbundle
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
And inside my registration action i have:
/**
* @Route("/api/register")
* @Method({"POST"})
*/
public function registerAction(Request $request)
{
$userManager = $this->get('fos_user.user_manager');
$data = $request->request->all();
$mailValidator = $this->get('validator.email');
$mailValidator->validate($data['email']);
$user = $userManager->createUser();
$user->setUsername($data['username']);
$user->setPlainPassword($data['password']);
$user->setEmail($data['email']);
$user->setEnabled(true);
$userManager->updateUser($user);
return $this->generateToken($user, 201);
}
protected function generateToken(User $user, $statusCode = 200)
{
$token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user);
$response = array(
'token' => $token,
'refreshToken' => null,
'username' => $user->getUsername(),
'mail' => $user->getEmail(),
);
return new JsonResponse($response, $statusCode);
}
Inside generate action method i can create token from user entity, but i can't manage to create refresh token also. For provider i use FOSUserBundle, and it's login_check controller. I've tried to send post request to that controller from generateToken method, but didn't succeed. Any help would be appreciated.