title: LexikJWTAuthenticationBundle generate Token
body: <p>I am using the <strong>LexikJWTAuthenticationBundle</strong> to generate json token, my problem is that I don't know how I generate a token after verifying that the user is in BD. I am able to help?</p>
  
  <p>My Code.</p>
  
  <pre><code> public function postLoginAction(Request $request)
      {
          $user = new Utilizador();
          $json = json_decode($request-&gt;getContent(), true);
          $user-&gt;setUser($json['user']);
          $user-&gt;setPass($json['pass']);
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $existuser = $em-&gt;getRepository('RestBundle:Utilizador')-&gt;findOneBy(array(
              'user' =&gt; $user-&gt;getUser(),
              'pass' =&gt; $user-&gt;getPass()
          ));
          if($existuser)
      {
          $token =  $this-&gt;get('lexik_jwt_authentication.jwt_manager')-&gt;create($user);
          return new JsonResponse(['token' =&gt; $token]);
      }
  
      }
  </code></pre>
  
  <p>My Utilizador Class</p>
  
  <pre><code> &lt;?php
  
  namespace RestBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  
  /**
   * Utilizador
   *
   * @ORM\Entity
   * @ORM\Table(name="utilizador")
   */
  class Utilizador implements UserInterface
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="nome", type="string", length=100, nullable=true)
       */
      private $nome;
  
      /**
       * @var string
       *
       * @ORM\Column(name="user", type="string", length=100, nullable=true)
       */
      private $username;
  
      /**
       * @var string
       *
       * @ORM\Column(name="pass", type="string", length=100, nullable=true)
       */
      private $pass;
  
      /**
       * @var string
       *
       * @ORM\Column(name="email", type="string", length=100, nullable=true)
       */
      private $email;
  
  
  
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set nome
       *
       * @param string $nome
       *
       * @return Utilizador
       */
      public function setNome($nome)
      {
          $this-&gt;nome = $nome;
  
          return $this;
      }
  
      /**
       * Get nome
       *
       * @return string
       */
      public function getNome()
      {
          return $this-&gt;nome;
      }
  
      /**
       * Set user
       *
       * @param string $username
       *
       * @return Utilizador
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      /**
       * Get user
       *
       * @return string
       */
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      /**
       * Set pass
       *
       * @param string $pass
       *
       * @return Utilizador
       */
      public function setPassword($pass)
      {
          $this-&gt;pass = $pass;
  
          return $this;
      }
  
      /**
       * Get pass
       *
       * @return string
       */
      public function getPassword()
      {
          return $this-&gt;pass;
      }
  
      /**
       * Set email
       *
       * @param string $email
       *
       * @return Utilizador
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
      /**
       * Get email
       *
       * @return string
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
      public function getRoles()
      {
          return array('ROLE_USER');
      }
  
      public function eraseCredentials()
      {
      }
      public function getSalt()
      {
          return null;
      }
  }
  </code></pre>
  
  <p>Security.yml</p>
  
  <pre><code>security:
  
      # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
      providers:
          in_memory:
              memory: ~
  
      firewalls:
  
          login:
              pattern:  ^/api/logins
              stateless: true
              anonymous: true
  
          api:
              pattern:   ^/api
              stateless: true
              lexik_jwt: ~
  
      access_control:
          - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }
  </code></pre>
  
  <p>Another API Class</p>
  
  <pre><code>&lt;?php
  /**
   * Created by PhpStorm.
   * Date: 08/08/16
   * Time: 18:28
   */
  
  namespace RestBundle\Controller;
  
  
  use RestBundle\Form\StatusType;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use FOS\RestBundle\Controller\FOSRestController;
  use Symfony\Component\HttpKernel\Exception\HttpException;
  
  use RestBundle\Entity\Status;
  class StatusController extends FOSRestController
  {
  
      public function getStatusAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $user = $em-&gt;getRepository('RestBundle:Status')-&gt;findAll();
  
          return $user;
      }
  
      public function getStatuAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $user = $em-&gt;getRepository('RestBundle:Status')-&gt;find($id);
  
          if (!$id) {
              throw new HttpException(400, "Invalid id");
          }
  
  
          return $user;
      }
  
      public function postStatusAction(Request $request)
      {
          $user = new Status();
          $json = json_decode($request-&gt;getContent(), true);
          $user-&gt;setNome($json['nome']);
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($user);
          $em-&gt;flush();
          return $user;
      }
  
  }
  </code></pre>