Establishing global credentials in VueJS using Axios

When working in main.js, I included the following code:

axios.defaults.withCredentials = true;

Although this should make it work, I found that the cookies were not being sent to the back end.

I inspected the request header and noticed something strange:

https://i.stack.imgur.com/DOsGh.png

After some testing, I discovered that adding {withCredentials: true} in a specific request made it work perfectly.

However, I wanted a way to avoid having to add this option in each individual request. I decided to set up an interceptor to examine the options being sent with every request.

Have you encountered a similar issue before? Any suggestions on how I can avoid adding this option to each request?

Thank you for your help!

https://i.stack.imgur.com/yBseX.png

Answer №1

Axios offers the functionality of interceptors as well. Here's how you can set it up:

axios.interceptors.request.use(
  function(config) {
    // Perform an action before the request is sent
    config.withCredentials = true;
    return config;
  },
  function(error) {
    // Handle any errors with the request
    return Promise.reject(error);
  }
);

Answer №2

After much consideration, I made the decision to stop using axios and switch to vue-resource. By implementing an interceptor with credentials set to true in my main.js file, everything is now running smoothly.

Here is the code snippet from main.js :

Vue.http.interceptors.push(function(request) {
  request.credentials = true
  return function(response) {
  };
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Vue Testing Library with Nuxt.js: A Beginner's Guide

Looking to incorporate Vue Testing Library into my Nuxt.js project. Encountered an error right after installation, where running a test results in the following message: 'vue-cli-service' is not recognized as an internal or external command, op ...

Unable to save cookies on mobile browsers, but functioning properly on desktop computers

Currently, I am facing an issue with my ExpressJS app hosted on Heroku and a frontend React app hosted on Netlify. The problem arises when a user logs in successfully and is directed to the home page showing personalized content. Upon landing on the home p ...

Is it possible for pdfjs-dist to be used with Typescript?

Is there a way to preview a PDF as a canvas without importing pdfjs-dist into my project? I have already used the command $yarn add pdfjs-dist to install pdfjs-dist. Do I need to include any additional imports? import pdfjsLib from "pdfjs-dist/build ...

How to leverage Vue Selectize to fetch JSON data from dropdown options?

I have integrated vue2-selectize to showcase a list of options fetched via an axios call: <template> <selectize v-model="selected" :settings="settings"> <option v-for="option in options" :value="option.id"> ({{ op ...

Sending multiple arguments to a Vuex action

In the Vue Component code snippet below, I have a method: loadMaintenances (query = {}) { this.getContractorMaintenances(this.urlWithPage, query).then((response) => { this.lastPage = response.data.meta.last_page }) } I am trying to pass the par ...

Issue with setting cookie in Codeception and Selenium

My Selenium test is failing when trying to set a cookie, despite reducing the code to its simplest form. It seems that setting cookies is not working at all in this case. /** * Test if we can set simple cookies * * @param \AcceptanceTester $i * ...

Is it possible to implement sanctum on multiple Vue projects simultaneously?

Greetings! I am currently using Sanctum to authenticate users in the system. My goal is for a user who logs in to vue project num1 to also be automatically logged in to vue project num2. I attempted to implement this functionality using cookies like so: $ ...

omitting post request using axios in react js with redux form (does not transmit any data in post request)

I am currently facing an issue where I am trying to make a post request using axios, to send a form to the backend. However, when I use the post request, nothing is being sent. I am utilizing redux-form to capture the form data and axios to send it to a Fl ...

Guide to extracting a key from the route module with the help of vuex-router-sync

I have a Vuex store setup as shown below, and I am using vuex-router-sync to keep it in sync. This plugin adds a router module to my store. However, I am unsure of how to retrieve this object from the store since there are no associated getters with this m ...

Dynamic Component Interactions in VueJS using Mouse Events

Just starting out with Vue and other frameworks, so my approach may not be very "Vue-like". I am attempting to create a versatile button component that can have different behaviors based on a prop, in order to maintain just one button component. The desir ...

Vue.js - computed property not rendering in repeated list

It seems like the issue lies in the timing rather than being related to asynchronous operations. I'm currently iterating through an object and displaying a list of items. One of the values requires calculation using a method. While the direct values ...

Is there something lacking in my project.json while building with nuxtjs?

Issue detected in ./services/emailService.js Unable to locate module: Error: Unable to find '@/apis/adl/instance/EmailsApi' in I encountered this error while executing the nuxt build within a docker container or on certain CI/CD servers such a ...

Utilizing Chart.js with Vue for dynamic time axis plots from JSON data through computed properties

Trying to generate a chart with a time axis in Vue using Chart.js has been a challenge. Initially, everything worked perfectly when the data was set directly in the Data object as shown below: chartData: { datasets: [ { backgroundC ...

Error encountered with Vuetify stepper simple component wrapper and v-on="$listeners" attribute

I'm working on developing a custom wrapper for the Vuetify Stepper component with the intention of applying some personalized CSS styles to it. My aim is to transmit all the $attrs, $listeners, and $slots. I do not wish to alter any functionality or ...

Is it possible to extract information from a form's POST request without relying on the traditional "action" attribute within form elements?

Last night, I was experimenting with ExpressJS and discovered something interesting when working with a simple code snippet: app.post('/contact', function(req, res, next) { res.send('Congratulations! You have submitted the form'); }) ...

Struggling with my `welcome.blade.php` not detecting Vue imported within my Nuxt.js directory

I have been working with Nuxt.js on my Laravel project. Combining them was a necessary step since they each come with their own boilerplate during installation into the project. I managed to make almost everything work smoothly until I encountered an issu ...

stopping Vue from aggressively recycling DOM elements

Consider the snippet below: <template v-if="tryIsMobile" > <div class='device device-mobile-portrait' :class="deviceClass"> <div class="device-scroller-container"> <div class='device-scroller'> ...

Problem encountered when attempting to utilize the spread operator within .vue files in an Elixir Phoenix 1.3 application

I'm facing an issue while building Vue.js components that involve using the spread operator to map states from Vuex within my Phoenix 1.3 application. The JavaScript compile errors I encountered are causing some roadblocks: 26 | }, 27 | compu ...

Display the full price when no discount is available, but only reveal the discounted price when Vue.js is present

In my collection of objects, each item is structured like this: orders : [ { id: 1, image: require("./assets/imgs/product1.png"), originalPrice: 40, discountPrice: "", buyBtn: require(&q ...

Issue with Framework7 toggle component's reactivity being ineffective

I am currently utilizing Vue and Framework7 for the development of an android app. Within a list, I have 4 toggles where only one can be active at a time. The active toggle must also be disabled to ensure that there is always at least one active toggle. ...