npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tshio/redux-api-auth-middleware

v2.0.1

Published

This little tool will help you work with request authorization and token renewal.

Downloads

347

Readme

Redux API Auth Middleware

This little tool will help you work with request authorization and token renewal.

Use it to add Authentication header to your API requests and/or refresh your expired JWT/OAuth/custom tokens.

Installation

Using yarn:

$ yarn add @tshio/redux-api-auth-middleware

Using npm:

$ npm install --save @tshio/redux-api-auth-middleware

Then, to enable authMiddleware, use applyMiddleware:

import { applyMiddleware, createStore } from 'redux';

import { apiMiddleware } from 'redux-api-middleware';
import { createAuthMiddleware } from '@tshio/redux-api-auth-middleware';

import { appReducer } from 'app/app.reducer'; // your main reducer

// See config details below.
const authMiddleware = createAuthMiddleware({
  authConfig: {
    header: 'Authorization',
    type: 'Bearer',
  },
  refreshConfig: {
    endpoint: '/refresh-token',
    failedAction: { type: 'LOGOUT' },
    actionDefinition: ({ refreshToken, endpoint }) => refreshTokenAction(refreshToken, endpoint), //not required
  },
});
const middlewares = [authMiddleware, apiMiddleware];
const store = createStore(appReducer, applyMiddleware(...middlewares));

and add authReducer to your app:

import { combineReducers } from 'redux'
import { createAuthReducer } from '@tshio/redux-api-auth-middleware';

// See config details below.
const authReducer = createAuthReducer({
  getExpirationTimestamp: payload => payload.expiration,
});

export default combineReducers({
  auth: authReducer,
  ... // rest of your reducers
})

Usage

To use this middleware you have to save your accessToken and refreshToken using setTokenAction.

import { setTokenAction } from '@tshio/redux-api-auth-middleware';
import Component from './component';

const mapDispatchToProps = dispatch => ({
  onSignIn: ({accessToken, refreshToken}) => dispatch(setTokenAction({ access_token: accessToken, refresh_token: refreshToken, expires_in: 1555055916 }));
}

export default connect(
  null,
  mapDispatchToProps,
)(Component);

After that all you need to do is to dispatch an RSAA action, like the one below.

import { RSAA } from 'redux-api-middleware';

[RSAA]: {
  types: ['GET_USERS_API_REQUEST', 'GET_USERS_API_SUCCESS', 'GET_USERS_API_FAILURE'],
  endpoint: '/users',
  method: 'GET',
},

If for some reason you want to skip the middleware you have to add skipAuth key to your action.

import { RSAA } from 'redux-api-middleware';

[RSAA]: {
  types: ['LOGIN_REQUEST', 'LOGIN_SUCCESS', 'LOGIN_FAILURE'],
  endpoint: '/auth/login',
  method: 'POST',
  body: {
    username: '[email protected]',
    pass: 'terces',
  },
  skipAuth: true,
},

To clear the token use clearTokenAction.

import { clearTokenAction } from '@tshio/redux-api-auth-middleware';
import Component from './component';

const mapDispatchToProps = dispatch => ({
  onSignOut: () => dispatch(clearTokenAction());
}

export default connect(
  null,
  mapDispatchToProps,
)(Component);

Configuration

Auth middleware

| Key | Option name | Default value | Type | Role | | ------------- | ---------------- | ------------- | ------------- | ------------------------------------------------------------------------------ | | authConfig | - | - | - | Configuration for adding authorization headers | | | header | Authorization | string | Name of the header passed to every request that needs authorization | | | type | Bearer | string | Type of the token | | refreshConfig | - | - | - | Configuration for token refresh | | | endpoint | undefined | string | API endpoint for token renewal | | | failedAction | undefined | ReduxAction | Action that will be dispatched after failed token request | | | actionDefinition | undefined | ReduxAction | Function that will return RSAA Action that will be dispatched to refresh token |

Auth reducer

| Option name | Default value | Type | Role | | ---------------------- | ------------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------- | | getExpirationTimestamp | calculateJWTTokenExpirationDate | function<number> | Function returning expiration timestamp for requested token. Defaults to a function that sums iat and exp keys from JWT payload |

There are two built in functions for calculating token expiration timestamp, one for JWT and the other one for OAuth2. See examples below.

JWT Example

Function takes payload below, parses the access_token and looks for iat and exp keys to sum them. If they are not there it returns 0.

API Payload

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjQxMzM5ODA3OTksImV4cCI6MzYwMH0.XzogySsPK2_KU4uceVR1rwwKa31_5Ur9zhqCaBYVzUw",
  "refresh_token": "..."
}

Reducer Configuration

import { calculateJWTTokenExpirationDate } from '@tshio/redux-api-auth-middleware';

const authReducer = createAuthReducer({
  getExpirationTimestamp: calculateJWTTokenExpirationDate,
});

OAuth2 Example

Function takes payload below and adds expires_in value to current timestamp.

API Payload

{
  "access_token": "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3",
  "token_type": "bearer",
  "expires_in": 3600,
  "refresh_token": "IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk",
  "scope": "create"
}

Reducer Configuration

import { calculateOauthTokenExpirationDate } from '@tshio/redux-api-auth-middleware';

const authReducer = createAuthReducer({
  getExpirationTimestamp: calculateOauthTokenExpirationDate,
});