Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3521499
D10884.id36513.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.id36513.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;
@@ -85,24 +86,25 @@
const olmSessionCreator = React.useContext(OlmSessionCreatorContext);
invariant(olmSessionCreator, 'Olm session creator should be set');
- const [canPerformAuth, setCanPerformAuth] = React.useState(true);
- const isUserAuthenticated = useSelector(isLoggedInToKeyserver(keyserverID));
+ const [authInProgress, setAuthInProgress] = React.useState(true);
+ const performAuth = React.useCallback(() => {
+ setAuthInProgress(false);
- React.useEffect(() => {
- if (
- !usingCommServicesAccessToken ||
- !canPerformAuth ||
- isUserAuthenticated
- ) {
- return;
- }
- setCanPerformAuth(false);
+ let cancelled = false;
+ const cancel = () => {
+ cancelled = true;
+ setAuthInProgress(true);
+ };
- void (async () => {
+ 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 +118,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 +130,102 @@
? { [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) {
+ 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);
- setCanPerformAuth(true);
+ if (!cancelled) {
+ await sleep(AUTH_RETRY_DELAY_MS);
+ setAuthInProgress(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 (!hasAccessToken) {
+ cancelPendingAuth.current?.();
+ cancelPendingAuth.current = null;
+ }
+
+ if (
+ !usingCommServicesAccessToken ||
+ isUserAuthenticated ||
+ !hasAccessToken
+ ) {
+ return;
+ }
+
+ if (prevPerformAuth.current !== performAuth) {
+ cancelPendingAuth.current?.();
+ cancelPendingAuth.current = null;
+ }
+ prevPerformAuth.current = performAuth;
+
+ if (!authInProgress) {
+ return;
+ }
+
+ const [, cancel] = performAuth();
+ cancelPendingAuth.current = cancel;
+ }, [authInProgress, hasAccessToken, isUserAuthenticated, performAuth]);
+
if (keyserverID !== ashoatKeyserverID) {
return null;
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Dec 24, 3:41 AM (18 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2698191
Default Alt Text
D10884.id36513.diff (5 KB)
Attached To
Mode
D10884: [lib] Cancel an auth every time dependencies change
Attached
Detach File
Event Timeline
Log In to Comment