Symfony 2 : manual login and Json Web Token
After a lot of search (google, stackoverflow, sample applications, bundle's doc , ...), it seems that there was no proposed solution to authenticate users manually from a controller.
Also, I had to open the source code, find which method is called to generate the token on successful authentication event (source code), and finally adapt the code to my need (Register/Login user to the API from the response of a Facebook login, in a mobile application).
My alternative :
// SecurityController.phpprotected function generateToken($user, $statusCode = 200){ // Call jwt_manager service & create the token $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user); // If you want, add some user informations $userInformations = array( 'id' => $user->getId(), 'username' => $user->getUsername(), 'email' => $user->getEmail(), 'roles' => $user->getRoles(), ); // Build your response $response = array( 'token' => $token, 'user' => $user, ); // Return the response in JSON format return new JsonResponse($response, $statusCode);}
The token will be returned same as the classic login_check handler, and have the same time before expiration.
Hope this help for next users.