Axios-module: axios withCredentials not being passed from nuxt.config.js

Created on 19 Oct 2018  路  10Comments  路  Source: nuxt-community/axios-module

i had a problem that withCredentials was not being picked up from the nuxt.config.js

  axios: {
    withCredentials: true,
    baseURL: process.env.DATA_API 
  },

so i added an axios plugin with

export default function({ $axios, redirect }) {
  $axios.onRequest(config => {
    config.withCredentials = true

    return config
  })
}

and everything worked for me,
any idea what i was doing wrong?

This question is available on Nuxt community (#c175)
question

Most helpful comment

You are not doing anything wrong, it is a bug. I had the same problem and resolved similarly. But that is a serious issue and needs to be reopened

All 10 comments

This issue as been imported as question since it does not respect axios-module issue template. Only bug reports and feature requests stays open to reduce maintainers workload.
If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically.
Your question is available at https://cmty.app/nuxt/axios-module/issues/c175.

You are not doing anything wrong, it is a bug. I had the same problem and resolved similarly. But that is a serious issue and needs to be reopened

This is still an issue. Here is a quick workaround, you can do in nuxt.config:

axios: {
    credentials: true,
    init(axios) {
      axios.defaults.withCredentials = true
    }
  } 

@daniel-payne @husayt
This is in the specification, not a bug.
According to documentation and source code, credentials option only work when axios request to baseURL or relative path.
see:
https://axios.nuxtjs.org/options#credentials
https://github.com/nuxt-community/axios-module/blob/dev/lib/plugin.js#L80

@tockn

I'm having the same issue. Without
axios: { withCredentials: true }
no requests to baseUrl + /path/to/endpoint have credentials. Adding the above-mentioned code piece to nuxt.config.js helps, however another issue takes place, which I've described here https://cmty.app/nuxt/axios-module/issues/c221#comment-5c855a0c4f9b2d0bea516c6f.

Interestingly:

  1. With axios: {withCredentials: true} SSR requests don't have the credentials, but all the requests that take place when moving from one page to another - i.e. on the client - do have the credentials.
  2. Without axios: {withCredentials: true} no requests have credentials set.

I"m having the same issue with SSR, any luck?

@daukadolt any solutions?

It's worth pointing out that for post requests with a payload, the solutions mentioned here are not enough (at least they weren't in my case). There's an open issue in the Axios repo (see here) - basically, you have to manually add the withCredentials: true when calling axios.post

I handle all my API communication via a custom ApiService (instead of directly via axios) and the exposed methods are really simple. So, for post requests, just making sure that the withCredentials: true is present in the last argument is what does the trick:

  public async post<T>(url: string, body: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
    return axios.post(`${this.baseUrl}/${url}`, body, { ...{ withCredentials: true }, ...config });
  }

i had a problem that withCredentials was not being picked up from the nuxt.config.js

  axios: {
    withCredentials: true,
    baseURL: process.env.DATA_API 
  },

so i added an axios plugin with

export default function({ $axios, redirect }) {
  $axios.onRequest(config => {
    config.withCredentials = true

    return config
  })
}

and everything worked for me,
any idea what i was doing wrong?

_This question is available on Nuxt community (#c175)_

I has to add this plugin to nuxt.config.js auth options as well, to allow cookies to be set from login endpoint.
/* ** Auth strategies */ auth: { plugins: ['@plugins/axios.js'], // This ensures that we can set cookies from API } ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaboume picture kaboume  路  4Comments

tebaly picture tebaly  路  3Comments

daolou picture daolou  路  3Comments

gammpamm picture gammpamm  路  4Comments

ealves-pt picture ealves-pt  路  3Comments