Skip to content

Instantly share code, notes, and snippets.

@ream88
Last active October 26, 2015 13:12
Show Gist options
  • Select an option

  • Save ream88/013e5536923d46c809e0 to your computer and use it in GitHub Desktop.

Select an option

Save ream88/013e5536923d46c809e0 to your computer and use it in GitHub Desktop.
Proof of concept how to do a basic authentication middleware for Redux
let previousAction = {};
let delayedActions = [];
const delayedNext = action => {
delayedActions.push(action);
return;
};
const authorization = () => next => action => {
switch(action.type) {
// Only used for testing, maybe a better way could be found.
case "@@authorization/RESET":
delayedActions = [];
return;
case "AUTHORIZATION_FAILURE":
delayedActions.push(previousAction);
return next(action);
case "AUTHORIZATION_SUCCESS":
const lastAction = delayedActions.pop();
next(action);
while(delayedActions.length)
next(delayedActions.shift());
return next(lastAction);
default:
previousAction = action;
return !delayedActions.length ? next(action) : delayedNext(action);
}
};
export default authorization;
/*eslint-env mocha*/
import assert from "assert";
import authorization from ".";
describe("authorization", () => {
let dispatchedActions, expectedActions;
const actionHandler = authorization()(action => {
const expectedAction = expectedActions.shift();
assert.deepEqual(action, expectedAction);
});
it("delays all actions after an AUTHORIZATION_FAILURE is dispatched", () => {
dispatchedActions = [
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "AUTHORIZATION_FAILURE" },
{ type: "FETCH_PAGES_REQUEST", id: 2 }
];
expectedActions = [
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "AUTHORIZATION_FAILURE" }
];
dispatchedActions.forEach(action => actionHandler(action));
assert(!expectedActions.length);
});
it("re-dispatches failed action after an AUTHORIZATION_SUCCESS is dispatched", () => {
dispatchedActions = [
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "AUTHORIZATION_FAILURE" },
{ type: "AUTHORIZATION_SUCCESS" }
];
expectedActions = [
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "AUTHORIZATION_FAILURE" },
{ type: "AUTHORIZATION_SUCCESS" },
{ type: "FETCH_PAGES_REQUEST", id: 1 }
];
dispatchedActions.forEach(action => actionHandler(action));
assert(!expectedActions.length);
});
it("dispatches all stored actions after an AUTHORIZATION_SUCCESS is dispatched", () => {
dispatchedActions = [
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "AUTHORIZATION_FAILURE" },
{ type: "FETCH_PAGES_REQUEST", id: 2 },
{ type: "FETCH_PAGES_REQUEST", id: 3 },
{ type: "AUTHORIZATION_SUCCESS" }
];
expectedActions = [
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "AUTHORIZATION_FAILURE" },
{ type: "AUTHORIZATION_SUCCESS" },
{ type: "FETCH_PAGES_REQUEST", id: 1 },
{ type: "FETCH_PAGES_REQUEST", id: 2 },
{ type: "FETCH_PAGES_REQUEST", id: 3 }
];
dispatchedActions.forEach((action) => actionHandler(action));
assert(!expectedActions.length);
});
afterEach(() => actionHandler({ type: "@@authorization/RESET" }));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment