-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Reauthentication.js
95 lines (82 loc) · 3.95 KB
/
Reauthentication.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import lodashGet from 'lodash/get';
import CONST from '../../CONST';
import * as NetworkStore from '../Network/NetworkStore';
import * as MainQueue from '../Network/MainQueue';
import * as Authentication from '../Authentication';
import * as PersistedRequests from '../actions/PersistedRequests';
import * as Request from '../Request';
import Log from '../Log';
/**
* Reauthentication middleware
*
* @param {Promise} response
* @param {Object} request
* @param {Boolean} isFromSequentialQueue
* @returns {Promise}
*/
function Reauthentication(response, request, isFromSequentialQueue) {
return response
.then((data) => {
// If there is no data for some reason then we cannot reauthenticate
if (!data) {
Log.hmmm('Undefined data in Reauthentication');
return;
}
if (NetworkStore.isOffline()) {
// If we are offline and somehow handling this response we do not want to reauthenticate
throw new Error('Unable to reauthenticate because we are offline');
}
if (data.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
// There are some API requests that should not be retried when there is an auth failure like
// creating and deleting logins. In those cases, they should handle the original response instead
// of the new response created by handleExpiredAuthToken.
const shouldRetry = lodashGet(request, 'data.shouldRetry');
if (!shouldRetry) {
if (isFromSequentialQueue) {
return data;
}
if (request.resolve) {
request.resolve(data);
}
return;
}
// We are already authenticating
if (NetworkStore.isAuthenticating()) {
if (isFromSequentialQueue) {
// This should never happen in theory. If we go offline while we are Authenticating or handling a response with a 407 jsonCode then isAuthenticating should be
// set to false. If we do somehow get here, we will log about it since we will never know it happened otherwise and it would be difficult to diagnose.
const message = 'Cannot complete sequential request because we are already authenticating';
Log.alert(`${CONST.ERROR.ENSURE_BUGBOT} ${message}`);
throw new Error(message);
}
MainQueue.replay(request);
return data;
}
return Authentication.reauthenticate(request.commandName)
.then((authenticateResponse) => {
if (isFromSequentialQueue) {
return Request.processWithMiddleware(request, true);
}
MainQueue.replay(request);
return authenticateResponse;
})
.catch(() => {
if (isFromSequentialQueue) {
throw new Error('Unable to reauthenticate sequential queue request because we failed to reauthenticate');
}
// If we make it here, then our reauthenticate request could not be made due to a networking issue. The original request can be retried safely.
MainQueue.replay(request);
});
}
if (isFromSequentialQueue) {
PersistedRequests.remove(request);
return data;
}
if (request.resolve) {
request.resolve(data);
}
// Return response data so we can chain the response with the following middlewares.
return data;
});
}
export default Reauthentication;