Page MenuHomePhabricator

D10884.id36384.diff
No OneTemporary

D10884.id36384.diff

diff --git a/lib/components/keyserver-connection-handler.js b/lib/components/keyserver-connection-handler.js
--- a/lib/components/keyserver-connection-handler.js
+++ b/lib/components/keyserver-connection-handler.js
@@ -34,6 +34,7 @@
};
const AUTH_RETRY_DELAY_MS = 60_000;
+const CANCELLED_ERROR = 'cancelled';
function KeyserverConnectionHandler(props: Props) {
const { socketComponent: Socket, keyserverID, ...rest } = props;
@@ -86,23 +87,23 @@
invariant(olmSessionCreator, 'Olm session creator should be set');
const [canPerformAuth, setCanPerformAuth] = React.useState(true);
- const isUserAuthenticated = useSelector(isLoggedInToKeyserver(keyserverID));
-
- React.useEffect(() => {
- if (
- !usingCommServicesAccessToken ||
- !canPerformAuth ||
- isUserAuthenticated
- ) {
- return;
- }
+ const performAuth = React.useCallback(() => {
setCanPerformAuth(false);
- void (async () => {
+ let cancelled = false;
+ const cancel = () => {
+ cancelled = true;
+ };
+
+ const promise = (async () => {
try {
const keyserverKeys =
await identityClient.getKeyserverKeys(keyserverID);
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+
const [notifsSession, contentSession] = await Promise.all([
olmSessionCreator.notificationsSessionCreator(
cookie,
@@ -116,6 +117,10 @@
),
]);
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+
const { userID, deviceID } = await getAuthMetadata();
invariant(userID, 'userID should be set');
invariant(deviceID, 'deviceID should be set');
@@ -124,54 +129,97 @@
? { [keyserverID]: { deviceToken } }
: {};
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+
await dispatchActionPromise(
keyserverAuthActionTypes,
- keyserverAuth({
- userID,
- deviceID,
- doNotRegister: false,
- calendarQuery,
- deviceTokenUpdateInput,
- logInActionSource: process.env.BROWSER
- ? logInActionSources.keyserverAuthFromWeb
- : logInActionSources.keyserverAuthFromNative,
- keyserverData: {
- [keyserverID]: {
- initialContentEncryptedMessage: contentSession,
- initialNotificationsEncryptedMessage: notifsSession,
+ (async () => {
+ await keyserverAuth({
+ userID,
+ deviceID,
+ doNotRegister: false,
+ calendarQuery,
+ deviceTokenUpdateInput,
+ logInActionSource: process.env.BROWSER
+ ? logInActionSources.keyserverAuthFromWeb
+ : logInActionSources.keyserverAuthFromNative,
+ keyserverData: {
+ [keyserverID]: {
+ initialContentEncryptedMessage: contentSession,
+ initialNotificationsEncryptedMessage: notifsSession,
+ },
},
- },
- }),
+ });
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+ })(),
);
} catch (e) {
+ if (cancelled) {
+ setCanPerformAuth(true);
+ return;
+ }
+
console.log(
`Error while authenticating to keyserver with id ${keyserverID}`,
e,
);
+
if (!dataLoaded && keyserverID === ashoatKeyserverID) {
await dispatchActionPromise(logOutActionTypes, callLogOut());
}
} finally {
- await sleep(AUTH_RETRY_DELAY_MS);
+ if (!cancelled) {
+ await sleep(AUTH_RETRY_DELAY_MS);
+ }
setCanPerformAuth(true);
}
})();
+ return [promise, cancel];
}, [
- keyserverID,
- identityClient,
- olmSessionCreator,
- cookie,
- getAuthMetadata,
- dispatchActionPromise,
- keyserverAuth,
- deviceToken,
calendarQuery,
- isUserAuthenticated,
callLogOut,
+ cookie,
dataLoaded,
- canPerformAuth,
+ deviceToken,
+ dispatchActionPromise,
+ getAuthMetadata,
+ identityClient,
+ keyserverAuth,
+ keyserverID,
+ olmSessionCreator,
]);
+ const cancelPendingAuth = React.useRef<?() => void>(null);
+ const prevPerformAuth = React.useRef(performAuth);
+ const isUserAuthenticated = useSelector(isLoggedInToKeyserver(keyserverID));
+ const hasAccessToken = useSelector(state => !!state.commServicesAccessToken);
+
+ React.useEffect(() => {
+ if (
+ !usingCommServicesAccessToken ||
+ isUserAuthenticated ||
+ !hasAccessToken
+ ) {
+ return;
+ }
+
+ if (prevPerformAuth.current !== performAuth) {
+ cancelPendingAuth.current?.();
+ }
+ prevPerformAuth.current = performAuth;
+
+ if (!canPerformAuth) {
+ return;
+ }
+
+ const [, cancel] = performAuth();
+ cancelPendingAuth.current = cancel;
+ }, [canPerformAuth, hasAccessToken, isUserAuthenticated, performAuth]);
+
if (keyserverID !== ashoatKeyserverID) {
return null;
}

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 6, 9:38 PM (20 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2432628
Default Alt Text
D10884.id36384.diff (5 KB)

Event Timeline