Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axios withCredentials not being passed from nuxt.config.js #168

Closed
daniel-payne opened this issue Oct 19, 2018 · 11 comments
Closed

axios withCredentials not being passed from nuxt.config.js #168

daniel-payne opened this issue Oct 19, 2018 · 11 comments

Comments

@daniel-payne
Copy link

daniel-payne commented Oct 19, 2018

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)
@ghost ghost closed this as completed Oct 19, 2018
@ghost ghost added the cmty:question label Oct 19, 2018
@ghost
Copy link

ghost commented Oct 19, 2018

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.

@husayt
Copy link

husayt commented Nov 24, 2018

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

@husayt husayt reopened this Feb 11, 2019
@husayt
Copy link

husayt commented Feb 11, 2019

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
    }
  } 

@tockn
Copy link
Contributor

tockn commented Mar 11, 2019

@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

@daukadolt
Copy link

@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.

@Faisal0sal
Copy link

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

@skylight-ity
Copy link

@daukadolt any solutions?

@lukaVarga
Copy link

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 });
  }

@ricardogobbosouza
Copy link
Member

@pi0 can closed?
See #168 (comment)

@syntheticgoo
Copy link

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
  }
...```

@13protons
Copy link

Maybe someone finds this helpful - as a nuxt noob I never changed my async fetch calls to use this.$axios.$get (or $post, whatever). As soon as I did that in my vue components all worked as advertised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.