Skip to content

Latest commit

 

History

History
37 lines (23 loc) · 1.29 KB

README.md

File metadata and controls

37 lines (23 loc) · 1.29 KB

Axios JWT authentication interceptor

Tests

npm i axios-jwt-auth

Attach authorization token to outcoming requests. Intercept response errors, refresh if needed. Fully customizable.

100% TypeScript. No external dependencies.

Minimal configuration

/**
 * Do not use the same axios instance to refresh tokens as you do for your authenticated requests!.
 */
const refreshTokens = async (token: Token) => {
  const { data } = await axios.post<IAuthTokens>('/api/refresh', { token });

  return data;
};

const axiosInstance = axios.create();
const config: Config = { refreshTokens };

applyInterceptors(axiosInstance, config);

Additional configuration

By default localStorage is used to store auth tokens. You can provide your own storage by implementing ITokensStorage interface.

Tokens are refreshed if a request returns with 401 status. Override shouldRefresh to change this behavior.

Access token is attached to headers using Bearer scheme. Override applyAccessToken to change this behavior.

If refresh tokens requests fails an optional onFailedToRefresh is fired with the request's error object as argument.