0

How to make requests from a page behind the auth middleware to an external API using axios? the API doesn't need any authentication.
Every time requests are sent containing auth token, it doesn't make any problems but I don't feel safe to send auth tokens to an external API each time. I tried this so far:

const config = {
            headers: { Authorization: '' }
        };
        let response = $axios.$get(`APIURL`,config)

however header request still contains auth token.

Light
  • 13
  • 3

1 Answers1

0

You shouldn't use token header as default indeed.

Are You using axios instance in seperate file where You can configure the request/response config? So do so.

Set the default auth header only for specific url, for example base API url like http://api.localhost:3333 and send the token only there.

Example:

// Add a request interceptor

import axios from "axios";
axios.interceptors.request.use(config => {

const token = '';
   const apiUrl = https://api.xxdomain.com/;

   if (config.url.contains(apiUrl)) {
       config.headers['Authorization'] = 'Bearer ' + token;
   }else{
     config.headers['Content-Type'] = 'application/json';
   }

   return config;
},
error => {
   Promise.reject(error)
});

try to read also this post-> helpful link

devzom
  • 676
  • 1
  • 9
  • 19