Juliver Galleto
Juliver Galleto

Reputation: 9037

add axios authorization on nuxt config

How to add a global authorization header on nuxt.config.js?

tried

  axios: {
    defaults : {
      headers : {
        common: [
          {
            'Authorization' : '5fb9c42ceba425fb9c42ceba43'
          }
        ]
      }
    }
  },

but not working

I can do

this.$axios.setHeader('Authorization', this.$store.state.appstore.akey);

but I find it not ideal when having multiple axios request because I have to add it on every request

Upvotes: 0

Views: 4965

Answers (2)

Ilijanovic
Ilijanovic

Reputation: 14904

I have created an plugin for it. Goto plugins and create axios.js:

export default function ({ $axios, store }) {
  if (process.client) {
    $axios.setToken(store.state.appstore.akey, 'Bearer')
  }
}

Then register your plugin in nuxt.config.js

plugins: ['@/plugins/axios'],

Upvotes: 2

Nick Dawes
Nick Dawes

Reputation: 2244

Try this, taken from the module docs.

axios: {
  headers : {
    common: {
      'Authorization' : '5fb9c42ceba425fb9c42ceba43'
    }
  }
}

Upvotes: 3

Related Questions