title: Laravel JWT - Consuming your own API with Javascript
body: <p>I'm using Laravel 5.6 with <a href="https://github.com/tymondesigns/jwt-auth" rel="nofollow noreferrer">jwt-auth</a> for API Authentication. The idea behind it is simple:</p>
  
  <ol>
  <li>You send a HTTP POST request with user credentials and get an "access_token" if successfully. </li>
  <li>Every subsequent request should be sent with header "Authorization: Bearer {access_token}".</li>
  </ol>
  
  <p>This steps are cool and flexible if the consumer is from outside (e.g. Android App). </p>
  
  <p>But if the API consumer is my own JavaScript? Should I also login when starting each asynchronous request even if already knows the authenticated user in my web middleware? How people usually manage this?</p>
  
  <p>With Passport (OAuth2.0), Laravel delivers a middleware that injects a cookie with authorization token, making this process automatic and easy: </p>
  
  <blockquote>
    <p>Typically, if you want to consume your API from your JavaScript application, you would need to manually send an access token to the application and pass it with each request to your application. However, Passport includes a middleware that can handle this for you. All you need to do is add the <code>CreateFreshApiToken</code> middleware to your web middleware group in your <code>app/Http/Kernel.php</code> file:</p>
  </blockquote>
  
  <pre><code>'web' =&gt; [
      // Other middleware...
      \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
  ],
  </code></pre>
  
  <p><a href="https://laravel.com/docs/5.6/passport#consuming-your-api-with-javascript" rel="nofollow noreferrer">Read more</a>.</p>
  
  <p>How to use JWT (tymondesigns/jwt-auth) and make requests to my own application's API without explicitly passing an access token?</p>