AxaFrance / oidc-client

Light, Secure, Pure Javascript OIDC (Open ID Connect) Client. We provide also a REACT wrapper (compatible NextJS, etc.).
MIT License
570 stars 152 forks source link

Use of Axios for Fetching #1354

Closed siang13 closed 2 months ago

siang13 commented 2 months ago

Issue and Steps to Reproduce

Can check is it possible to use Axios and service workers for fetching

kramer99 commented 2 months ago

You can use Axios interceptors to inject the access token, ie:

axios.interceptors.request.use(
  (config) => {
    const oidcStorage = localStorage.getItem('oidc.default');
    const { tokens } = JSON.parse(oidcStorage || '{}');

    /* eslint-disable no-param-reassign */
    config.baseURL = process.env.NEXT_PUBLIC_API_BASE;
    if (config.headers) {
      config.headers.Authorization = `Bearer ${tokens.accessToken}`;
    }
    return config;
  },
  (error) => Promise.reject(error)
);
siang13 commented 2 months ago

You can use Axios interceptors to inject the access token, ie:

axios.interceptors.request.use(
  (config) => {
    const oidcStorage = localStorage.getItem('oidc.default');
    const { tokens } = JSON.parse(oidcStorage || '{}');

    /* eslint-disable no-param-reassign */
    config.baseURL = process.env.NEXT_PUBLIC_API_BASE;
    if (config.headers) {
      config.headers.Authorization = `Bearer ${tokens.accessToken}`;
    }
    return config;
  },
  (error) => Promise.reject(error)
);

Is this working with service workers as well? Because the token can only be accessed by the service worker.

siang13 commented 2 months ago

Service worker will work with Axios or fetch without useOidcFetch hooks. Thank you.