Coder Social home page Coder Social logo

Comments (3)

Flyrell avatar Flyrell commented on May 22, 2024

Would you please provide a small replication for this? At first glance it looks good, so it's hard to tell where the problem lies. Which version of the plugin are you using? It might also be helpful to see the call which causes 401.

from axios-auth-refresh.

tadapp avatar tadapp commented on May 22, 2024

Thank you for a quick response!

I'm just a fool - I used the wrong syntax for my variables)
This works great!
Just in case, I will publish the correct code - perhaps this will be an example of a quick start for others.

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify';
import axios from 'axios';
import createAuthRefreshInterceptor from 'axios-auth-refresh';

Vue.config.productionTip = false;
Vue.prototype.$http = axios;
Vue.prototype.$serverApiLink = 'http://xsph.ru/api/';

function getAccessToken(){
    return store.state.authTokens.access
}


// Function that will be called to refresh authorization
const refreshAuthLogic = failedRequest =>
    axios.get(Vue.prototype.$serverApiLink+'auth/token/new_access', {headers: {Refresh: store.state.authTokens.refresh}}).then(tokenRefreshResponse => {
    window.console.log(store.state.authTokens.refresh);
    store.commit('userAuthTokens', tokenRefreshResponse.data.tokens);
    failedRequest.response.config.headers['Authorization'] = tokenRefreshResponse.data.tokens.access;
    return Promise.resolve();
}).catch(error => (
        window.console.log(error.response)
    ));
// Instantiate the interceptor (you can chain it as it returns the axios instance)
createAuthRefreshInterceptor(axios, refreshAuthLogic);
axios.interceptors.request.use(request => {
    request.headers['Authorization'] = getAccessToken();
    return request;
});

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount('#app');

from axios-auth-refresh.

Flyrell avatar Flyrell commented on May 22, 2024

Ok. I will close the issue then. Please, consider using StackOverflow for the future implementation help. Thank you.

from axios-auth-refresh.

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.