Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request interceptor containing an async call #33

Open
stokedout opened this issue Feb 25, 2019 · 3 comments
Open

Request interceptor containing an async call #33

stokedout opened this issue Feb 25, 2019 · 3 comments

Comments

@stokedout
Copy link

Hi, Is it possible to perform asynchronous calls in a "request" interceptor? I have an interceptor that gets the access token from the localforage library but all methods are asynchronous. If so could you provide a code example please? Thanks

@Vassi
Copy link

Vassi commented May 15, 2019

You might have realized this already but it already works this way:

this.unregisterFetch = fetchIntercept.register({
  request: async (url, config = {}) => {
    const fetchOptions = {
      ...config,
    };

    try {
      const token = await this.Authenticator.getIdToken();

      if (!fetchOptions.headers) {
        fetchOptions.headers = new Headers();
      }
      fetchOptions.headers.append('Authorization', `Bearer ${token}`);
    } catch (error) {
      // Should we re-login or inspect error here?
    }

    return [url, fetchOptions];
  },
});

If you're using something like adal/angular-adal note that my getIdToken is just a wrapper around their ridiculous synchronous/callback call that promisifies it.

@Ray0401
Copy link

Ray0401 commented Sep 23, 2021

You might have realized this already but it already works this way:

this.unregisterFetch = fetchIntercept.register({
  request: async (url, config = {}) => {
    const fetchOptions = {
      ...config,
    };

    try {
      const token = await this.Authenticator.getIdToken();

      if (!fetchOptions.headers) {
        fetchOptions.headers = new Headers();
      }
      fetchOptions.headers.append('Authorization', `Bearer ${token}`);
    } catch (error) {
      // Should we re-login or inspect error here?
    }

    return [url, fetchOptions];
  },
});

If you're using something like adal/angular-adal note that my getIdToken is just a wrapper around their ridiculous synchronous/callback call that promisifies it.

This way does not take effect

@Ray0401
Copy link

Ray0401 commented Sep 23, 2021

Hi, Is it possible to perform asynchronous calls in a "request" interceptor? I have an interceptor that gets the access token from the localforage library but all methods are asynchronous. If so could you provide a code example please? Thanks

I have the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants