Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redux-token-api-middleware

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-token-api-middleware - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

.node-version

25

lib/index.js

@@ -15,2 +15,3 @@ 'use strict';

exports.shouldRequestNewToken = shouldRequestNewToken;
exports.createApiAction = createApiAction;
exports.actionAsPromise = actionAsPromise;

@@ -37,2 +38,4 @@ exports.createTokenApiMiddleware = createTokenApiMiddleware;

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

@@ -139,3 +142,2 @@

this.meta = this.apiAction.meta || {};
// console.log('dispatch', dispatch)
this.dispatch = dispatch;

@@ -203,7 +205,3 @@ this.config = config;

// console.log(action, token);
var apiFetchArgs = this.getApiFetchArgsFromActionPayload(action.payload, token);
// console.log('apiFetchArgs');
// console.log(apiFetchArgs);
// console.log(this);
this.dispatch(createStartAction(action.type, action.payload));

@@ -259,5 +257,3 @@ return this.apiRequest(apiFetchArgs, action, this.store);

var _addTokenToRequest = this.addTokenToRequest(headers, endpoint, body, token);
// console.log('token & authenticate');
headers = _addTokenToRequest.headers;

@@ -274,3 +270,3 @@ endpoint = _addTokenToRequest.endpoint;

if (this.shouldRequestNewToken) {
if (this.shouldRequestNewToken()) {
var refreshAction = this.refreshAction(this.token);

@@ -286,4 +282,2 @@ var refreshApiAction = refreshAction[CALL_TOKEN_API];

} else {
// console.log('no refresh');
// console.log(this.token);
return this.curriedApiCallMethod(this.token);

@@ -322,4 +316,7 @@ }

function createApiAction(action) {
return _defineProperty({}, CALL_TOKEN_API, action);
}
function actionAsPromise(action, dispatch, config) {
// console.log('actionAsPromise', dispatch)
var apiAction = action()[CALL_TOKEN_API];

@@ -337,9 +334,5 @@ if (apiAction) {

// console.log('createTokenApiMiddleware');
return function (store) {
return function (next) {
return function (action) {
var apiAction = action[CALL_TOKEN_API];

@@ -351,4 +344,2 @@

// console.log(apiAction.type);
var tokenApiService = new TokenApiService(apiAction, store.dispatch, config);

@@ -355,0 +346,0 @@

{
"name": "redux-token-api-middleware",
"version": "0.3.0",
"version": "0.4.0",
"description": "Redux middleware for calling APIs with token-based auth",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -18,4 +18,3 @@ # redux-token-api-middleware

Actions that will be intercepted by the middleware are identified by the symbol
`CALL_TOKEN_API`, and follow the [Flux Standard Action]() pattern. They must have
at least an `endpoint` in the payload.
`CALL_TOKEN_API`, and follow the [Flux Standard Action](https://github.com/acdlite/flux-standard-action) pattern, however they must have a payload and have at least an `endpoint` in the payload.

@@ -64,2 +63,14 @@ #### Examples

// example refresh token action
const refreshToken = (token) => {
return {
[CALL_TOKEN_API]: {
type: 'REFRESH_TOKEN',
endpoint: 'http://localhost/token',
method: 'POST',
body: JSON.stringify(token)
}
}
}
const config = {

@@ -75,2 +86,3 @@ refreshAction: refreshToken

)
```

@@ -81,4 +93,54 @@ ## API

Creates a Redux middleware
Creates a Redux middleware to handle API objects.
In the config, you must define at least a `refreshAction` method, which is used by the middleware to attempt to get a fresh token. This should be an API action itself.
Other methods can be passed in via config, but have defaults:
```javascript
// defaults shown
const config = {
tokenStorageKey: 'reduxMiddlewareAuthToken',
minTokenLifespan: 300, // seconds (min remaining lifespan to indicate new token should be requested)
storeToken: function storeToken(key, response) {
let token = response.token;
localStorage.setItem(key, JSON.stringify(token));
return token;
},
retrieveToken: function retrieveToken(key) {
let storedValue = localStorage.getItem(key);
if (!storedValue) {
return null;
}
try {
return JSON.parse(storedValue);
}
catch (e) {
if (e instanceof SyntaxError) {
return null;
}
throw e;
}
},
checkTokenFreshness: function checkTokenFreshness(token) {
let tokenPayload = jwt.decode(token);
let expiry = moment.unix(tokenPayload.exp);
return expiry.diff(moment(), 'seconds') > MIN_TOKEN_LIFESPAN;
},
shouldRequestNewToken: function shouldRequestNewToken() {
const token = retrieveToken();
return token
? checkTokenFreshness(token)
: false;
},
addTokenToRequest: function defaultAddTokenToRequest(headers, endpoint, body, token) {
return {
headers: Object.assign({
Authorization: `JWT ${token}`
}, headers),
endpoint,
body
}
}
}
```

@@ -105,3 +105,2 @@ import _ from 'lodash';

this.meta = this.apiAction.meta || {};
// console.log('dispatch', dispatch)
this.dispatch = dispatch;

@@ -177,9 +176,5 @@ this.config = config;

apiCallFromAction(action, token=null) {
// console.log(action, token);
const apiFetchArgs = this.getApiFetchArgsFromActionPayload(
action.payload, token
);
// console.log('apiFetchArgs');
// console.log(apiFetchArgs);
// console.log(this);
this.dispatch(createStartAction(action.type, action.payload));

@@ -239,3 +234,2 @@ return this.apiRequest(apiFetchArgs, action, this.store);

if (token && authenticate) {
// console.log('token & authenticate');
(

@@ -253,3 +247,3 @@ { headers, endpoint, body } = this.addTokenToRequest(

call() {
if (this.shouldRequestNewToken) {
if (this.shouldRequestNewToken()) {
const refreshAction = this.refreshAction(this.token);

@@ -274,4 +268,2 @@ const refreshApiAction = refreshAction[CALL_TOKEN_API];

} else {
// console.log('no refresh');
// console.log(this.token);
return this.curriedApiCallMethod(this.token);

@@ -283,4 +275,9 @@ }

export function createApiAction(action) {
return {
[CALL_TOKEN_API]: action
}
}
export function actionAsPromise(action, dispatch, config) {
// console.log('actionAsPromise', dispatch)
const apiAction = action()[CALL_TOKEN_API];

@@ -296,7 +293,3 @@ if (apiAction) {

export function createTokenApiMiddleware(config={}) {
// console.log('createTokenApiMiddleware');
return store => next => action => {
const apiAction = action[CALL_TOKEN_API];

@@ -308,4 +301,2 @@

// console.log(apiAction.type);
const tokenApiService = new TokenApiService(

@@ -316,5 +307,3 @@ apiAction, store.dispatch, config

tokenApiService.call();
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc