4

I have been using nuxt/auth-next and axios modules with nuxt project since last 3-4 months, everything was working fine since yesterday but now whenever I try to send axios request to public APIs without passing Authorization in headers, I get this error

Cannot read property 'Authorization' of undefined with Nuxt Auth & Axios

Attached is a screenshot of the page

enter image description here

below is my code in index.js store file

export const actions = {
  async nuxtServerInit({ commit }, context) {
    // Public profile
    if (context.route.params && context.route.params.subdomain) {
      context.$axios.onRequest((config) => {
        config.progress = false
      })

      let { data } = await context.$axios.get(
        `users/get_user_data_using_subdomain/${context.route.params.subdomain}`,
        {
          headers: {
            'Content-Type': 'multipart/form-data',
          },
        }
      )
      await context.store.dispatch('artists/setPublicProfile', data.user_data)
    }
  },
}
aslamdoctor
  • 3,753
  • 11
  • 53
  • 95
  • Can you try removing the headers property inside the `axios.get()` and add `context .$axios.setHeader('Content-Type', 'multipart/form-data')` just before `axios.get()`. I had a similar problem when sending token in the Authorization header. – Luis Miranda Apr 21 '21 at 14:30
  • I have the same problem. It seems to only happen when using Auth und Axios together. Without Auth I would not get this error. Any news on this? – chrs Jan 01 '22 at 16:30

2 Answers2

0

This happend to me to when I was using context.app.$axios instead of context.$axios within a injection

Darkshifty
  • 343
  • 5
  • 12
0

Nuxt server is looking for config.headers.common.Authorization.

The example below is a quick win for you:

let { data } = await context.$axios.get(
  `users/get_user_data_using_subdomain/${context.route.params.subdomain}`,
  {
    headers: {
      common: null, // or something like this: context.$axios.defaults.headers?.common
      'Content-Type': 'multipart/form-data',
    },
  }
)
nuvola
  • 1