|
1 |
| -import type {OnyxEntry} from 'react-native-onyx'; |
| 1 | +import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; |
2 | 2 | import Onyx from 'react-native-onyx';
|
3 | 3 | import * as ActiveClientManager from '@libs/ActiveClientManager';
|
4 | 4 | import Log from '@libs/Log';
|
| 5 | +import * as NetworkStore from '@libs/Network/NetworkStore'; |
5 | 6 | import * as SequentialQueue from '@libs/Network/SequentialQueue';
|
6 | 7 | import * as App from '@userActions/App';
|
| 8 | +import updateSessionAuthTokens from '@userActions/Session/updateSessionAuthTokens'; |
7 | 9 | import ONYXKEYS from '@src/ONYXKEYS';
|
8 |
| -import type {OnyxUpdatesFromServer} from '@src/types/onyx'; |
| 10 | +import type {OnyxUpdatesFromServer, Session} from '@src/types/onyx'; |
9 | 11 | import {isValidOnyxUpdateFromServer} from '@src/types/onyx/OnyxUpdatesFromServer';
|
10 | 12 | import * as OnyxUpdateManagerUtils from './utils';
|
11 | 13 | import * as DeferredOnyxUpdates from './utils/DeferredOnyxUpdates';
|
@@ -90,6 +92,10 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromSer
|
90 | 92 | return;
|
91 | 93 | }
|
92 | 94 |
|
| 95 | + // Check if one of these onyx updates is for the authToken. If it is, let's update our authToken now because our |
| 96 | + // current authToken is probably invalid. |
| 97 | + updateAuthTokenIfNecessary(onyxUpdatesFromServer); |
| 98 | + |
93 | 99 | const updateParams = onyxUpdatesFromServer;
|
94 | 100 | const lastUpdateIDFromServer = onyxUpdatesFromServer.lastUpdateID;
|
95 | 101 | const previousUpdateIDFromServer = onyxUpdatesFromServer.previousUpdateID;
|
@@ -144,6 +150,29 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromSer
|
144 | 150 | DeferredOnyxUpdates.getMissingOnyxUpdatesQueryPromise()?.finally(finalizeUpdatesAndResumeQueue);
|
145 | 151 | }
|
146 | 152 |
|
| 153 | +function updateAuthTokenIfNecessary(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromServer>): void { |
| 154 | + // Consolidate all of the given Onyx updates |
| 155 | + const onyxUpdates: OnyxUpdate[] = []; |
| 156 | + onyxUpdatesFromServer?.updates?.forEach((updateEvent) => onyxUpdates.push(...updateEvent.data)); |
| 157 | + onyxUpdates.push(...(onyxUpdatesFromServer?.response?.onyxData ?? [])); |
| 158 | + |
| 159 | + // Find any session updates |
| 160 | + const sessionUpdates = onyxUpdates?.filter((onyxUpdate) => onyxUpdate.key === ONYXKEYS.SESSION); |
| 161 | + |
| 162 | + // If any of the updates changes the authToken, let's update it now |
| 163 | + sessionUpdates?.forEach((sessionUpdate) => { |
| 164 | + const session = (sessionUpdate.value ?? {}) as Session; |
| 165 | + const newAuthToken = session.authToken ?? ''; |
| 166 | + if (!newAuthToken) { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + Log.info('[OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.'); |
| 171 | + updateSessionAuthTokens(newAuthToken); |
| 172 | + NetworkStore.setAuthToken(newAuthToken); |
| 173 | + }); |
| 174 | +} |
| 175 | + |
147 | 176 | export default () => {
|
148 | 177 | console.debug('[OnyxUpdateManager] Listening for updates from the server');
|
149 | 178 | Onyx.connect({
|
|
0 commit comments