Adding API key in authorization header using rewrites in nodejs

56 views Asked by At

An API I was using changed to use auth tokens, and directly sending requests to the API from my frontend results in CORS issues.

From what I've gathered, I can use rewrites to proxy my request and add the authorization header in the rewrite?

The following is in my next.config.js:

module.exports = {
  async rewrites() {
    return [
      {
        source: '/API',
        destination: 'API URL',
        headers: [
          {
            key: 'accept',
            value: 'application/json',
          },
          {
            key: 'Authorization',
            value: 'Bearer ' + process.env.KEY,
          },
        ],
          
      },
    ];
  },
};

For some reason the string operation to generate the header doesn't work.

Solution attempts:

  • Passing the header directly in the request -> CORS error.
  • Code above. Expectation: I assign the result of the string operation, which should be "Bearer [KEY]", as the value for the Authorization header and can make API requests. Result: String operation is apparently invalid here?
0

There are 0 answers