Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/atayahmet/react-axios-token-provider
React Axios Token Provider package is a make easy what repetitive processes.
https://github.com/atayahmet/react-axios-token-provider
access-token csrf-token management provider react react-native refresh-token xsrf
Last synced: about 1 month ago
JSON representation
React Axios Token Provider package is a make easy what repetitive processes.
- Host: GitHub
- URL: https://github.com/atayahmet/react-axios-token-provider
- Owner: atayahmet
- License: mit
- Created: 2020-03-03T19:30:25.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-26T14:58:04.000Z (over 1 year ago)
- Last Synced: 2024-04-28T16:22:16.326Z (8 months ago)
- Topics: access-token, csrf-token, management, provider, react, react-native, refresh-token, xsrf
- Language: TypeScript
- Homepage:
- Size: 68.4 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/atayahmet/react-axios-token-provider.svg?branch=master)](https://travis-ci.org/atayahmet/react-axios-token-provider)
# 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`
```sh
$ npm i @atayahmet/react-axios-token-provider --save
``````sh
$ yarn add @atayahmet/react-axios-token-provider
```## Basic Usage
```jsx
import AxiosTokenProvider from '@atayahmet/react-axios-token-provider';
import axios from 'axios';function App() {
return (
Hello World!
);
}
```## 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](#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:**
```tsx
import axios from 'axios';
;
```## init
The init prop is a inilizer function for provide extra config to developers.
**Example:**
```js
function initializer(instance) {
instance.baseURL = 'https://reqres.in/api';
}
``````tsx
```
## tokenPathVariants
You can define all token (access, refresh or csrf) paths to this prop.
**Example:**
```tsx
```
## Default Path Variants
```js
{
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:**
```js
function unauthorized(response) {
location.href = '/login';
}function forbidden(response) {
// do something
}
``````tsx
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
## License
[MIT](https://choosealicense.com/licenses/mit/)