title: How to log user out with Axios using authorization bearer + token
body: <p>I have an application where I log user in by connecting to backend (using axios) and getting a JWT Token which is stored in localStorage. My problem comes in with the logout functionality. I currently do this:</p>
  <pre><code>
  async endAuth(context) {
      try {
        await axios.post(&quot;auth/logout&quot;, {
          headers: {
           Authorization:
              &quot;Bearer &quot; + JSON.parse(localStorage.getItem(&quot;currentUser&quot;)).token,
          },
        });
        localStorage.clear();
        context.commit(&quot;setUser&quot;, {
          token: null,
          userId: null,
        });
      } catch (e) {
        const error = new Error(&quot;Something went wrong&quot;);
        throw error;
      }
  }
  </code></pre>
  <p>This does not work. The connection to API is established but i get a 401 error from backend with
  <code>{success: false, message: 'Expired or Invalid Token'}</code></p>
  <p>Now I tried adding <code>&quot;Content-Type&quot;: &quot;application/json&quot;,</code> to the headers still no luck.</p>
  <p>I also changed my code to use JavaScript build in <code>fetch()</code> API. And that WORKS! Here is the code:</p>
  <pre><code>async endAuth(context) {
    let url = baseURL + &quot;/auth/logout&quot;;
    const response = await fetch(url, {
        method: &quot;POST&quot;,
      headers: {
        &quot;Content-Type&quot;: &quot;application/json&quot;,
        Authorization:
          &quot;Bearer &quot; + JSON.parse(localStorage.getItem(&quot;currentUser&quot;)).token,
      },
      });
      const responseData = await response.json();
      if (!response.ok) {
        const error = new Error(
          responseData.message || &quot;Failed to authenticate. Check your login data.&quot;
        );
        throw error;
      } else {
        localStorage.clear();
        context.commit(&quot;setUser&quot;, {
          token: null,
          userId: null,
        });
      }
  }
  </code></pre>
  <p>I also tried setting axios headers via <code>axios.defaults.headers.common[&quot;Authorization&quot;] = &quot;Bearer &quot; + localStorage.getItem(&quot;currentUser&quot;).token;</code> - no luck
  Am I missing something here with the axios request?</p>