Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3158225
D10884.id36384.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D10884.id36384.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D10884: [lib] Cancel an auth every time dependencies change
Attached
Detach File
Event Timeline
Log In to Comment