-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SignInRedirect.js
66 lines (59 loc) · 2.57 KB
/
SignInRedirect.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
import Onyx from 'react-native-onyx';
import ONYXKEYS from '../../ONYXKEYS';
import * as MainQueue from '../Network/MainQueue';
import DateUtils from '../DateUtils';
import * as Localize from '../Localize';
import * as PersistedRequests from './PersistedRequests';
import NetworkConnection from '../NetworkConnection';
let currentIsOffline;
let currentShouldForceOffline;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (network) => {
if (!network) {
return;
}
currentIsOffline = network.isOffline;
currentShouldForceOffline = Boolean(network.shouldForceOffline);
},
});
/**
* @param {String} errorMessage
*/
function clearStorageAndRedirect(errorMessage) {
// Under certain conditions, there are key-values we'd like to keep in storage even when a user is logged out.
// We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting
// flashes of unwanted default state.
const keysToPreserve = [];
keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE);
keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS);
// After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it.
// If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out.
if (currentIsOffline && !currentShouldForceOffline) {
keysToPreserve.push(ONYXKEYS.NETWORK);
}
Onyx.clear(keysToPreserve)
.then(() => {
if (!errorMessage) {
return;
}
// `Onyx.clear` reinitializes the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set`
Onyx.merge(ONYXKEYS.SESSION, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal(errorMessage)}});
});
}
/**
* Cleanup actions resulting in the user being redirected to the Sign-in page
* - Clears the Onyx store - removing the authToken redirects the user to the Sign-in page
* - Cancels pending network calls - any lingering requests are discarded to prevent unwanted storage writes
*
* Normally this method would live in Session.js, but that would cause a circular dependency with Network.js.
*
* @param {String} [errorMessage] error message to be displayed on the sign in page
*/
function redirectToSignIn(errorMessage) {
MainQueue.clear();
PersistedRequests.clear();
NetworkConnection.clearReconnectionCallbacks();
clearStorageAndRedirect(errorMessage);
}
export default redirectToSignIn;