i18next / i18next-xhr-backend

[deprecated] can be replaced with i18next-http-backend
https://github.com/i18next/i18next-http-backend
MIT License
253 stars 75 forks source link

Add authorization in request header #301

Open anatel opened 5 years ago

anatel commented 5 years ago

Is it possible to configure the request header and add authorization token before it is sent?

jamuhl commented 5 years ago

good catch...should have documented those options: https://github.com/i18next/i18next-xhr-backend/blob/master/src/ajax.js#L55

  // custom request headers sets request.setRequestHeader(key, value)
  customHeaders: {
    authorization: 'foo',
    // ...
  },
anatel commented 5 years ago

Thanks, I have a token that can change from one call to another, so I can't just pass a string, i want to pass it something dynamic, maybe a function.

I understand that loadPath can get a function, can customHeaders get a function as well?

Or is there any way to change this middleware's config after the init phase?

jamuhl commented 5 years ago

You should be able to modify backend.options, something like:

i18next.services.backendConnector.backend.options.customHeaders = { .... }

anatel commented 5 years ago

Great, thanks!

MatthewTrout commented 5 years ago

This still isn't ideal, could it be made to accept a headers function also? Trying to make this as declarative as possible..