-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
75 lines (63 loc) · 2.4 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { MAKE_REQUEST, recieveGrant } from '../actions';
import oauth, { EXPIRED_GRANT } from '../util/oauth';
// authMiddleware is a stateful piece of redux middleware which
// signs MAKE_REQUEST actions with a grant. If the grant is invalid,
// it will detain the request until a valid grant is obtained.
export default function createAuthMiddleware() {
const detainedActions = [];
let fetchingNewGrant = false;
return ({ dispatch, getState /*, subscribe */ }) => next => action => {
const authState = getState().auth;
if (action.type == MAKE_REQUEST) {
console.log('[auth] Signing request');
if (readGrant(getState) === EXPIRED_GRANT) {
console.log('[auth] Grant has expired, obtaining a new one');
detainActionAndObtainNewGrant(action, dispatch, getState);
}
else {
next(sign(action, getState));
}
}
else {
next(action);
}
}
function detainActionAndObtainNewGrant(action, dispatch, getState) {
detainedActions.push(action);
oauth.authorize()
.then((newGrant) => {
const recieveGrantAction = recieveGrant(newGrant);
// Middleware has completed the grant refresh flow, we now need to place
// the new grant in the store (so future requests will be signed using it)
console.log('[auth] Got new grant, updating store via dispatch', recieveGrantAction);
dispatch(recieveGrantAction);
// Now we want to sign the detained requests with the *new* grant
// (taking the value from the store); however, the `recieveGrantAction`
// is currently stuck in the `delayMiddleware`.
const grantInStoreAfterDispatch = readGrant(getState);
// This is where I would like to subscribe to the store.
//
// let unsubscribe = subscribe(() => {
// if (readGrant(getState) === VALID_GRANT) {
// unsubscribe();
// global.__endTest(grantInStoreAfterDispatch);
// }
// });
//
// End the test here (see test/issue-922.spec.js)
if (typeof global.__endTest === 'function') {
console.log('end test');
global.__endTest(grantInStoreAfterDispatch);
}
})
}
function readGrant(getState) {
return getState().auth.grant;
}
function sign(action, getState) {
return {
...action,
authorization: `Bearer ${readGrant(getState)}`
}
}
}