Intercept Axios request with global JS methods

markoffden

I have a site with web components based architecture, where each web component may be a separate Vue app with it's own API layer integrated via Axios. I need to implement Auth middleware for all HTTP requests, coming from either root app or web component app. I cannot use Axios built-in interceptors mechanism as there will be multiple instances of Axios. Is there a way I can do it with global JS methods? I know there is some browser extension based API out there, but that doesn't seem like something I am looking for.

markoffden

Just in case anybody else is interested, I have solved it with service worker. You can subscribe to fetch events and respond according to your auth logics. Your service worker code will look something like following:

self.addEventListener('fetch', async (event) => {
    const isAuthorised = await checkIsAuthorized(); // your auth API layer
    if (!isAuthorised) {
        const response = new Response(null, {
            status: 401,
            statusText: 'Unauthorised',
        });
        event.respondWith(response);
        return;
    }
    event.respondWith(fetch(event.request));
});

Service worker is able to intercept axios requests from shadow DOM as well, so it's a good match for web components case.

Besides, there is a nice article by Bartosz Polnik on implementing auth layer using service worker.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Global Keyboard intercept input

分類Dev

Failed to compile vue.js app due to axios get request

分類Dev

Delete request with axios - React

分類Dev

GraphQL post request in axios

分類Dev

Webflux, How to intercept a request and add a new header

分類Dev

Postman request with 'global' URL

分類Dev

to make a global means in a request

分類Dev

request.jsをaxiosに変換します(Vonage api)

分類Dev

How do I call a function on a PHP page from an JS Axios request

分類Dev

Vue.js Axios: unable to update page after HTTP GET request

分類Dev

sending array data with multipart/form-data post request in Axios vue.js

分類Dev

Axios createError.js:16 Uncaught (in promise) Error: Request failed with status code 400

分類Dev

Angular: intercept ajax request, check internet connection and not send request

分類Dev

Axios post request failing with a 404

分類Dev

Modifying Dalvik Virtual Machine to intercept methods of Application code

分類Dev

VueJS Axios pass Id token to a global header

分類Dev

Karate - Setting global request headers

分類Dev

How to intercept web url from chrome and add headers to the request?

分類Dev

Axios request returns error 500 (laravel, axios & vuejs)

分類Dev

Upload file to another server with request or axios nodejs

分類Dev

Then is not a function on axios async/await post request

分類Dev

Dynamic dropdown list react axios get request

分類Dev

How to get value from Axios request?

分類Dev

Axios post spotify api request return 400

分類Dev

axios post request to MongoDB Atlas error 11000

分類Dev

axios get request Error: Request failed with status code 504

分類Dev

NodeJS Express - Global Unique Request Id

分類Dev

Vue.jsアプリのどこでaxios.interceptors.request.useを呼び出すのですか?

分類Dev

Axios Vue Js:このオブジェクトの値を取得してAPIに表示する方法get request url

Related 関連記事

  1. 1

    Global Keyboard intercept input

  2. 2

    Failed to compile vue.js app due to axios get request

  3. 3

    Delete request with axios - React

  4. 4

    GraphQL post request in axios

  5. 5

    Webflux, How to intercept a request and add a new header

  6. 6

    Postman request with 'global' URL

  7. 7

    to make a global means in a request

  8. 8

    request.jsをaxiosに変換します(Vonage api)

  9. 9

    How do I call a function on a PHP page from an JS Axios request

  10. 10

    Vue.js Axios: unable to update page after HTTP GET request

  11. 11

    sending array data with multipart/form-data post request in Axios vue.js

  12. 12

    Axios createError.js:16 Uncaught (in promise) Error: Request failed with status code 400

  13. 13

    Angular: intercept ajax request, check internet connection and not send request

  14. 14

    Axios post request failing with a 404

  15. 15

    Modifying Dalvik Virtual Machine to intercept methods of Application code

  16. 16

    VueJS Axios pass Id token to a global header

  17. 17

    Karate - Setting global request headers

  18. 18

    How to intercept web url from chrome and add headers to the request?

  19. 19

    Axios request returns error 500 (laravel, axios & vuejs)

  20. 20

    Upload file to another server with request or axios nodejs

  21. 21

    Then is not a function on axios async/await post request

  22. 22

    Dynamic dropdown list react axios get request

  23. 23

    How to get value from Axios request?

  24. 24

    Axios post spotify api request return 400

  25. 25

    axios post request to MongoDB Atlas error 11000

  26. 26

    axios get request Error: Request failed with status code 504

  27. 27

    NodeJS Express - Global Unique Request Id

  28. 28

    Vue.jsアプリのどこでaxios.interceptors.request.useを呼び出すのですか?

  29. 29

    Axios Vue Js:このオブジェクトの値を取得してAPIに表示する方法get request url

ホットタグ

アーカイブ