Auth Axios Enhancer

Package provides a helper function to handle everything related to the authorization. Thanks to it, you can modify the axios instance to add the authorization header, and to handle the token refreshing.

The instance modified in this way may be used in the createHeseyaApiService function.

import axios from 'axios'
import { enhanceAxiosWithAuthTokenRefreshing } from '@heseya/store-core'

const axiosInstance = enhanceAxiosWithAuthTokenRefreshing(axios.create(), {
  heseyaUrl: 'https://api.example.com',
  getAccessToken: () => localStorage.get('accessToken'),
  getRefreshToken: () => localStorage.get('refreshToken'),
  setAccessToken: (token: string) => localStorage.set('accessToken', token),
  setRefreshToken: (token: string) => localStorage.set('refreshToken', token),
})

Modified axios will try to refresh the access token every time the request fails with the 401 response code. If token refreshing will succeed, the request will be retried, otherwise axios will throw original error.

TIP

When token refreshing fails, not only the original error will be thrown, but also the config.onTokenRefreshError function will be called. You should use it to logout the user.

Config object

KeyDescription
heseyaUrlURL of the Heseya API
getAccessTokenFunction that returns the access token or a promise returning it
getRefreshTokenFunction that returns the refresh token or a promise returning it
setAccessTokenFunction that sets the access token
setRefreshTokenFunction that sets the refresh token
setIdentityTokenFunction that sets the identity token
onTokenRefreshErrorFunction that is called when token refreshing fails
shouldIncludeAuthorizationHeaderFunction that should return true if the authorization header needs be included in the request. By default it's being added to every request.