question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to eject manually

See original GitHub issue

The documentation doesn’t say how to combine axios-auth-refresh and custom error interceptors. There is only the line “interceptor id (in case you want to eject it manually)” How to eject it manually? When i use $axios.onError (from @nuxt/axios) with createAuthRefreshInterceptor it stop working.

Code in nuxtjs plugin file:

import createAuthRefreshInterceptor from 'axios-auth-refresh';
import { Api } from '@/api/api'; // Custom api layer 

export default function({ $axios, store, isServer, res, error }, inject) {
  $axios.onError(async errorObject => {
    const code = parseInt(errorObject.response && errorObject.response.status);
    let message = '';
    if (code === 404) {
      message = 'Page not found';
    }
    if (code === 422) {
      return { error: errorObject, data: errorObject.response.data };
    }
   ... // other codes
    error({ statusCode: code, message }); 

    return { error: errorObject, data: errorObject.response.data };
  });

  const refreshAuthLogic = failedRequest =>
    store.dispatch('global/refreshToken').then(tokenRefreshResponse => {
      failedRequest.response.config.headers['Authorization'] =
        'Bearer ' + tokenRefreshResponse.token;
      return Promise.resolve();
    });

  createAuthRefreshInterceptor($axios, refreshAuthLogic);

  const api = new Api($axios); 
  inject('api', api);

}

And it doesn’t work, but if remove the onError handler it will

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Flyrellcommented, Aug 6, 2020

Well, if you emit it manually, you woudn’t really need that library. But there’s a bit more logic for it to work properly, as you need a queue for requests that has been dispatched while the token is refreshing, etc.

I think it might work if you move the createAuthRefreshInterceptor above the onError (so it gets called after your own $axios.onError interceptor and instead of returning Promise.reject({ error: errorObject, data: errorObject.response.data }) you’d return the Promise.reject(errorObject);. Can you try that?

createAuthRefreshInterceptor(/* ... */);
$axios.onError(async errorObject => {
    const code = parseInt(errorObject.response && errorObject.response.status);
    let message = '';
    if (code === 404) {
      message = 'Page not found';
    }
    if (code === 401) {
      return Promise.reject(errorObject);
    }

    error({ statusCode: code, message });

    return Promise.reject({
      error: errorObject,
      data: errorObject.response.data,
    });
  });
1reaction
Flyrellcommented, Aug 5, 2020

I’m not exactly sure what you’re trying to achieve. Would you please edit your question with a code sample?

Manual ejection should be as simple as calling the following…

const refreshAuthIterceptorId = createAuthRefreshInterceptor(/* ... */);
axios.interceptors.response.eject(refreshAuthIterceptorId);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to eject a stuck disc from a PS4 console (US) - PlayStation
If a disc gets stuck in your PS4 console, you can manually eject it. Before you begin you need: A long Phillips screwdriver;...
Read more >
How to manually eject a stuck PS4 game - Asurion
Insert a screwdriver into the manual eject hole, just above the PS4 logo, and turn counterclockwise to release the disc. You'll have to...
Read more >
How to Manually Eject a Stuck Disc in a PlayStation 4 - iFixit
Use a Phillips screwdriver and rotate the screw counter-clockwise. This shouldn't require very much pressure and you should see the stuck disc slowly...
Read more >
How to manually eject the CD/DVD drive tray. | Sony USA
To manually eject the CD/DVD drive tray from a Sony® CD/DVD drive, insert a pointed object, such as a paper clip, into the...
Read more >
8 Ways to Force Eject a Disc from Your Mac's Optical Drive
Hold down the eject key; Drag the disc icon to your Mac's trash bin; Right-click on the disc icon and select eject; Press...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found