Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@atayahmet/react-axios-token-provider

atayahmet208MIT1.0.5TypeScript support: included

React Axios Token Provider package is a make easy what repetitive processes.

react, react-native, axios, jwt, axios token, axios auth token, access token, refresh token, csrf token, token manager, provider

readme

Build Status

React Axios Token Provider

React Axios Token Provider package is a make easy what repetitive processes. For example handle access token or refresh token management process easly.

Why

It can be preferred to reduce repetitive practices for authentication operations in projects.

Features

  • Access token management
  • Refresh token management
  • Csrf/Xsrf token management

Installation

Use the package manager yarn or npm to install react-axios-token-provider

$ npm i @atayahmet/react-axios-token-provider --save
$ yarn add @atayahmet/react-axios-token-provider

Basic Usage

import AxiosTokenProvider from '@atayahmet/react-axios-token-provider';
import axios from 'axios';

function App() {
  return (
    <AxiosTokenProvider instance={axios}>
      <div>
        <h1>Hello World!</h1>
      </div>
    </AxiosTokenProvider>
  );
}

Props

name type default description
instance AxiosInstance AxiosInstance An axios instance.
init Function - Initializer helper function.
refreshToken Boolean false Activation of refresh token.
csrfToken Boolean false Activation of csrf token.
initialAccessToken String - Initial access token.
initialRefreshToken String - Initial refresh token.
initialCsrfToken String - Initial csrf token.
tokenPathVariants IPathVariants Default Path Variants The paths of all type tokens in response object.
statusCallbacks Object - Specific events of status codes.

instance

You need to define your axios instance you want to manage. If no instance is defined, no action will be taken. A log is written to the console at the warning level.

Example:

import axios from 'axios';

<AxiosTokenProvider instance={axios}>
  <App />
</AxiosTokenProvider>;

init

The init prop is a inilizer function for provide extra config to developers.

Example:

function initializer(instance) {
  instance.baseURL = 'https://reqres.in/api';
}
<AxiosTokenProvider init={initializer} instance={axios}>
  <App />
</AxiosTokenProvider>

tokenPathVariants

You can define all token (access, refresh or csrf) paths to this prop.

Example:

<AxiosTokenProvider
  tokenPathVariants={{
    accessToken: ['headers.X-Access-Token', 'data.tokens.access_token'],
    refreshToken: ['headers.X-Refresh-Token', 'data.tokens.refresh_token'],
  }}
></AxiosTokenProvider>

Default Path Variants

{
  accessToken: ['headers.x-access-token', 'data.access_token'],
  csrfToken: ['headers.x-csrf-token', 'headers.x-xsrf-token'],
  refreshToken: ['headers.x-refresh-token', 'data.refresh_token'],
}

statusCallbacks

You can define specific callbacks to response status codes.

Example:

function unauthorized(response) {
  location.href = '/login';
}

function forbidden(response) {
  // do something
}
<AxiosTokenProvider
  statusCallbacks={{
    401: unauthorized,
    403: forbidden,
  }}
></AxiosTokenProvider>

Contributing

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

License

MIT

changelog

Release Notes:

Changes for v1.0.0-rc.2:

  • Docs improvement.
  • The tokens added to local state and prevented component rerender with shouldComponentUpdate hook.
  • Added csrf support.

Changes for v1.0.0-rc.3:

  • Made csrf header name changable.
  • Made authorization header name changable.
  • Made authorization header value prefix changable.
  • Path variants merge has become more practical.

Changes for v1.0.0-rc.4:

  • Docs improvement.
  • Changed package description.
  • Added condition that checking the existence of instance.

Changes for v1.0.0-rc.5:

  • Fixed wrong store key.
  • Added fallback to request interceptor when refresh key invalid (undefined or null)

Changes for v1.0.0-rc.6:

  • Added component unmount check for avoid memory leak error.

Changes for v1.0.0-rc.7:

  • Added component tests.
  • Added props types for plain javascript environment.
  • Fixed merge path variants function.
  • Added updater prop for update provider config without render the component.
  • Added async support to storage processes.

Changes for v1.0.0-rc.8:

  • Initial token and previously registered token added to the state according to the hierarchy.
  • If csrf token value is undfined, changed as null.

Changes for v1.0.0-rc.9:

  • Default value is assigned in case it is empty from getToken() method.

Changes for v1.0.1

  • Axios security fix