Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How to automatically purge cookies in the browser ?

Hello,

I have a Laravel application and I'm using Fortify to log in.

The front is with VueJS.

Today I have encountered a problem : the remote web server had a port forwarding problem and when I tried to connect, I got a 400 error (bad request).

Each time I tried to connect (with the server problem), a new session cookie was created and stored in the browser.

The result is that even when I solved the port forwarding, I couldn't log in unless I delete all cookies in the dev tools.

I don't want any client to have to do that.

How is it possible to detect this and purge all cookies only if I get a 400 error ?

Thanks for your help.

V

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To handle the automatic purging of cookies in the browser when a 400 error occurs, you can use JavaScript to detect the error and then clear the cookies. Since you are using Vue.js for the frontend, you can leverage Vue's lifecycle hooks and methods to achieve this.

Here's a step-by-step solution:

  1. Intercept the HTTP requests and responses: Use Axios interceptors to catch the 400 error.
  2. Clear the cookies: Use JavaScript to clear the cookies when the error is detected.

First, make sure you have Axios installed in your Vue.js project. If not, you can install it using npm:

npm install axios

Next, set up an Axios interceptor to catch the 400 error and clear the cookies. You can do this in a central place, such as in your main.js file or a dedicated Axios configuration file.

Here's an example of how you can achieve this:

import axios from 'axios';

// Function to clear all cookies
function clearCookies() {
    const cookies = document.cookie.split(";");

    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i];
        const eqPos = cookie.indexOf("=");
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
    }
}

// Set up Axios interceptor
axios.interceptors.response.use(
    response => {
        // If the response is successful, just return the response
        return response;
    },
    error => {
        // If the response is a 400 error, clear the cookies
        if (error.response && error.response.status === 400) {
            clearCookies();
        }
        // Return the error to be handled by the calling code
        return Promise.reject(error);
    }
);

// Now you can use axios in your Vue components as usual

In this example, the clearCookies function iterates over all cookies and sets their expiration date to a past date, effectively deleting them. The Axios interceptor checks for a 400 error in the response and calls clearCookies if such an error is detected.

You can now use Axios in your Vue components to make HTTP requests, and the interceptor will automatically handle the 400 error and clear the cookies if necessary.

Here's an example of how you might use Axios in a Vue component:

<template>
  <div>
    <button @click="login">Login</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  methods: {
    login() {
      axios.post('/login', {
        username: 'example',
        password: 'password'
      })
      .then(response => {
        console.log('Login successful:', response);
      })
      .catch(error => {
        console.error('Login failed:', error);
      });
    }
  }
};
</script>

With this setup, if a 400 error occurs during the login attempt, the cookies will be automatically cleared, and the user won't have to manually delete them.

1 like

Please or to participate in this conversation.