Giter VIP home page Giter VIP logo

Comments (5)

pi0 avatar pi0 commented on May 26, 2024 1

@andreliem Thanks for your suggestion actually context was available using this (Like this.store) but it was inconsistent with other use cases, so I've made context available as second argument (In 4.2.1) (docs).

Anyway, i agree with you. writing logic in the config is not a good idea, using middleware (Or plugins) is much better.

from axios-module.

momurad18 avatar momurad18 commented on May 26, 2024 1

I'm using auth module

export default function ({ $axios, store, redirect }) {
  $axios.onError((error) => {
    const code = parseInt(error.response && error.response.status)
    if (code === 503) {
      redirect('/maintenance')
    }
    if (code === 403) {
      redirect('/404')
    }
    if (code === 401) {
      store.$auth.logOut()
    }
  })
}

I added this in axios plugin work fine looking for better practice.

from axios-module.

andreliem avatar andreliem commented on May 26, 2024

I also tried using the errorHandler function to detect 401, which is working... but it seems like the store is not accessible from this function.

from axios-module.

andreliem avatar andreliem commented on May 26, 2024

Oh actually using middleware on its own without interceptors proved to be a good solution for me.

In case anyone is curious, I'm trying to set the session state of a user in the store before components load as I want specific components to hide/show content based on the store. Just redirecting to a login page isn't the UX I was looking to do.

export default async function ({ store }) {
    const authenticatedUser = await store.dispatch('authCheck')
    if (authenticatedUser) {
      store.dispatch('loginUser', authenticatedUser)
    } else {
      store.dispatch('logoutUser')
    }
}

from axios-module.

andreliem avatar andreliem commented on May 26, 2024

Thanks @pi0. Good to know there is the context available for 4.2.1 in the error handler, I actually think it's a pretty decent location to have logic which dispatches actions to the store when errors like session expiry occur... as opposed to having each component handle the error or through some global mixin perhaps.

from axios-module.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.