# axios-token-refresher

> Enables your axios client to attach a valid authorization token to each of your request.

Latest version **2.0.5** (published 2021-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install axios-token-refresher
pnpm add axios-token-refresher
yarn add axios-token-refresher
bun add axios-token-refresher
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2021-01-06 |
| First published | 2019-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.16.0 |
| Dependencies | 1 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | Ramesh Ramalingam |
| Maintainers | rrameshbtech |
| Keywords | axios, oauth, token, refresh, api, authorization |

## Links

- npm: https://www.npmjs.com/package/axios-token-refresher
- Repository: https://github.com/rrameshbtech/axios-token-refresher
- Homepage: https://github.com/rrameshbtech/axios-token-refresher#readme
- Issues: https://github.com/rrameshbtech/axios-token-refresher/issues
- npm.io page: https://npm.io/package/axios-token-refresher

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.21.1

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 2.0.5 (latest) — 2021-01-06
- 2.0.4 — 2019-09-18
- 2.0.3 — 2019-08-23
- 2.0.2 — 2019-07-31
- 2.0.1 — 2019-07-14
- 2.0.0 — 2019-06-20
- 1.0.2 — 2019-06-17
- 1.0.1 — 2019-06-17
- 1.0.0 — 2019-06-17

## README

# axios-token-refresher

[![Build Status](https://travis-ci.org/rrameshbtech/axios-token-refresher.svg?branch=master)](https://travis-ci.org/rrameshbtech/axios-token-refresher)

This library enables your axios client to append a valid authorization token to your requests.

## How to use?

Install the package using NPM as given below.

```
npm i axios-token-refresher
```

Then add the below code into your application.

```
const wrapTokenRefresher = require('axios-token-refresher');

const formatTokenResponse = (response) => ({
  {
      accessToken: response.token,
      expiresIn: response.expiry_duration, // in seconds
      tokenType: response.token_type // "Bearer" | "Basic"
    }
});

/*
This function fetches the new authorization token & returns the Promise
This function will be called by refresher to get new token whenever the existing
token is expired.
*/
const fetchAuthToken = () => axios
  .get('www.auth-server.com/get/token/')
  .then(response => formatTokenResponse(response.data));

/*
Optional token configurations. details below.
*/
const options = {
  invalidTokenStatuses : [401, 403]
}

const axiosClientWithToken = wrapTokenRefresher(axios.create(), fetchAuthToken, options);

```

Now use `axiosClientWithToken` as like normal axios client which will take care of refreshing & attaching valid auth token with your requests.

Note: We can import types from `axios-token-refresher/types` like given below.

`import { AuthToken } from "axios-token-refresher/types";`

## Options

Options can be passed as the third parameter for `wrapTokenRefresher`. It is optional.

```
const options = {

  // List of HTTP statuses which are sent by server when token is invalid.
  invalidTokenStatuses : [401], // default

  // Name of token header in which we send the fetched token.
  tokenHeaderName: 'authorization',  // default

  // `buildTokenHeader` allows to decide how the token header value should be built.
  buildTokenHeader: function(tokenDetails) {
    return `${tokenDetails.type} ${tokenDetails.value}`
  } // default
};

const axiosClientWithToken = wrapTokenRefresher(axios.create(), fetchAuthToken, options);

```

## Contribute

We welcome to contribute by adding features, fixing bugs or by creating feature requests or submitting issues.

Please contact me at rrameshbtech@gmail.com for more sugestions.

## License

This library is licensed under MIT License

---
_Source: https://npm.io/package/axios-token-refresher · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
