title: Expired JWT Token - How to refresh token
body: <p>I am developing a mobile application, and I realize that my token has a limited duration. I am using a Symfony server, which has a function to refresh me token:</p>
  
  <pre><code>/**
   * @Route("/api/refresh", name="api_refresh")
   */
  public function refreshTokenAction(Request $request)
  {
      if(!$request-&gt;headers-&gt;has('Authorization')) {
          return;
      }
  
      $extractor = new AuthorizationHeaderTokenExtractor(
          'Bearer',
          'Authorization'
      );
  
      $token = $extractor-&gt;extract($request);
  
      $encoder = $this-&gt;get('lexik_jwt_authentication.encoder');
  
      $data = $encoder-&gt;decode($token);
  
      if(!$data){
          return;
      }
  
      $username = $data['mail'];
  
      $user = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Benevole')
          -&gt;findOneBy(['mail' =&gt; $username]);
  
      $token = $this-&gt;get('lexik_jwt_authentication.encoder')
          -&gt;encode(['mail' =&gt; $user-&gt;getMail()]);
  
      // Return genereted tocken
      return new JsonResponse(['token' =&gt; $token]);
  
  }
  </code></pre>
  
  <p>I use this server with an application in AngularJS, in which I call my server in this way:</p>
  
  <pre><code>var refreshToken = function(idPatient){
      var token = window.localStorage.getItem('token'); // current valid token
      return $http({
             method  : 'GET',
             url     : url + '/refresh',
             headers : {Authorization : 'Bearer ' + token},
         }).then(function(result) {
          console.log(result.data);
          return result.data;
      });
  };
  </code></pre>
  
  <p>When I test my function refresh token, clicking a button, it works, the token is refreshed. </p>
  
  <p>I would like to know how I can refresh my token, this automatically, so that the user does not have to disconnect each time, because it is rather restrictive ^^
  Should I check my token every time ? Should I put several conditions for the application to find this every time ?</p>