Gomah/nuxt-graphql-request

Best way to set Bearer token dynamically in nuxt.config.js?

jimmiejackson414-zz opened this issue · 1 comments

Hey there,

Wondering what the best way is to set the Bearer token in nuxt.config.js?

I'm using another library called cookie-universal-nuxt to get/set cookies (where I'm storing the token). Here's what I currently have:

nuxt.config.js:

graphql: {
  endpoint: process.env.NUXT_ENV_BACKEND_API_URL,
  options: './apollo/config.js'
},

config.js:

export default ({ $cookies }) => {
  return {
      headers: {
        authorization: `Bearer: ${$cookies.get('gc_token')}`
      }
  };
};

This doesn't seem to be working at all though. Any ideas how to dynamically insert the token?

Ended up using the setHeaders method on the my default layout, which removed the need to set it in nuxt.config:

created () {
  const token = this.$cookies.get('gc_token');
  this.$graphql.setHeaders({ authorization: `Bearer ${token}` });
}