4

I'm trying to implement a MapLayer in my react application. The map is only accessible over a proxy which needs authentication to identify the users for each end every request.

To provide the token i added the following request interceptor to make sure all calls which access that backend would be expanded by the authorization. If i do it this way there is no header added to the requests.

esriConfig.request.interceptors.push({
  urls: ['backend/api/*'],
  before: async function (params: any) {
    params.requestOptions.headers = {
      Authorization: 'Bearer ' + token),
    };
  },
});

If i remove the urls parameter so that all routes are extended with the token I'll recieve some cors errors because the esri package also makes some calls to another api where i get blocked because i'm sending an authorization header which is not expected.

Other api: https://www.arcgis.com/sharing/rest/portals/self?f=json&culture=de-de

How can i make sure only requests to my api will be extended with the autorization header?

  • 1
    I think you have a mistake in the regular expression, you need to escape the bar like this, `'backend\/api'` in order to capture it. Btw I don't think you need the `*` – cabesuon Dec 02 '20 at 17:46

1 Answers1

0

The urls property takes a String or RegExp or an array of those. See https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html#RequestInterceptor

You're specifyng the url as a string (not a proper RegEx). You need to change

urls: ['backend/api/*'],

to

urls: [/backend\/api/],

or (without the array)

urls: /backend\/api/,
Bjorn Svensson
  • 815
  • 8
  • 21