1.1.3 • Published 1 year ago

token-refresher-module v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Token-refresher-module

This module is guaranteed to send 1 request for a refresh token even with parallel requests. It also provides methods for mixing Axios requests with any other types of requests (such as web sockets).

Installation

yarn add token-refresher-module

or

npm install token-refresher-module

Basic Usage

import axios from "axios";
import tokenRefresherModule from "token-refresher-module";

const axiosInstance = axios.create({
  withCredentials: true,
  baseURL: "https://example.com/api",
});

const validationBeforeRefresh = (error) => {
  if (error.response.headers.userbanned) {
    return false;
  }
  return true;
};

const refreshToken = async () => {
  const token = await axiosInstance.post("refresh-token");
  return token;
};

const afterTokenRefreshedSuccess = () => {
  console.log("Token refreshed success");
};

const afterTokenRefreshFailed = () => {
  console.log("Token refreshing failed");
};

tokenRefresherModule({
  axiosInstance,
  validationBeforeRefresh,
  refreshToken,
  afterTokenRefreshedSuccess,
  afterTokenRefreshFailed,
});

Util for socket connection (signalR)

Use such a utility in the catch block when you need a refresh token and try again.

import {
  addToQueueInPromiseWrapper,
  initTokenRefreshingAndExecuteAllQueue,
  isTokenRefreshing,
} from "token-refresher-module";

const refreshTokenOnSignalHubConnection = async (
  connectToServerWithSocketsFn
) => {
  let result;

  if (isTokenRefreshing) {
    result = await addToQueueInPromiseWrapper(connectToServerWithSocketsFn);
  } else {
    result = await initTokenRefreshingAndExecuteAllQueue(
      connectToServerWithSocketsFn
    );
  }

  return result;
};

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago