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 @@ -10,7 +10,7 @@ useLogOut, } from '../actions/user-actions.js'; import { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js'; -import { setSessionRecoveryInProgressActionType } from '../keyserver-conn/keyserver-conn-types.js'; +import { setNewSession } from '../keyserver-conn/keyserver-conn-types.js'; import { resolveKeyserverSessionInvalidation } from '../keyserver-conn/recovery-utils.js'; import { filterThreadIDsInFilterList } from '../reducers/calendar-filters-reducer.js'; import { @@ -23,6 +23,7 @@ import { OlmSessionCreatorContext } from '../shared/olm-session-creator-context.js'; import type { BaseSocketProps } from '../socket/socket.react.js'; import { logInActionSources } from '../types/account-types.js'; +import { genericCookieInvalidation } from '../types/session-types.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector, useDispatch } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; @@ -198,6 +199,25 @@ .sessionRecoveryInProgress, ); + const preRequestUserInfo = useSelector(state => state.currentUserInfo); + const sessionID = useSelector( + state => state.keyserverStore.keyserverInfos[keyserverID]?.sessionID, + ); + const preRequestUserState = React.useMemo( + () => ({ + currentUserInfo: preRequestUserInfo, + cookiesAndSessions: { + [keyserverID]: { + cookie, + sessionID, + }, + }, + }), + [preRequestUserInfo, keyserverID, cookie, sessionID], + ); + const preRequestUserStateRef = React.useRef(preRequestUserState); + preRequestUserStateRef.current = preRequestUserState; + const dispatch = useDispatch(); const urlPrefix = useSelector( state => state.keyserverStore.keyserverInfos[keyserverID]?.urlPrefix, @@ -218,7 +238,7 @@ const promise = (async () => { try { - const sessionChange = await resolveKeyserverSessionInvalidation( + const recoverySessionChange = await resolveKeyserverSessionInvalidation( dispatch, cookie, urlPrefix, @@ -230,16 +250,21 @@ // dispatch directly throw new Error(CANCELLED_ERROR); } + const sessionChange = + recoverySessionChange ?? genericCookieInvalidation; if ( - !sessionChange || sessionChange.cookieInvalidated || !sessionChange.cookie || !sessionChange.cookie.startsWith('user=') ) { - dispatch({ - type: setSessionRecoveryInProgressActionType, - payload: { sessionRecoveryInProgress: false, keyserverID }, - }); + setNewSession( + dispatch, + sessionChange, + preRequestUserStateRef.current, + null, + logInActionSources.cookieInvalidationResolutionAttempt, + keyserverID, + ); } } catch (e) { if (cancelled) { @@ -251,10 +276,14 @@ e, ); - dispatch({ - type: setSessionRecoveryInProgressActionType, - payload: { sessionRecoveryInProgress: false, keyserverID }, - }); + setNewSession( + dispatch, + genericCookieInvalidation, + preRequestUserStateRef.current, + null, + logInActionSources.cookieInvalidationResolutionAttempt, + keyserverID, + ); } finally { if (!cancelled) { setAuthInProgress(false); diff --git a/lib/shared/session-utils.js b/lib/shared/session-utils.js --- a/lib/shared/session-utils.js +++ b/lib/shared/session-utils.js @@ -62,6 +62,10 @@ 'COOKIE_INVALIDATION_RESOLUTION_ATTEMPT or ' + 'SOCKET_AUTH_ERROR_RESOLUTION_ATTEMPT login is dispatched', ); + if (actionCurrentUserInfo.anonymous) { + // It's not a session recovery if the CurrentUserInfo is anonymous + return false; + } return ( !currentReduxState.dataLoaded || currentReduxState.currentUserInfo?.id !== actionCurrentUserInfo.id diff --git a/lib/types/session-types.js b/lib/types/session-types.js --- a/lib/types/session-types.js +++ b/lib/types/session-types.js @@ -62,9 +62,16 @@ +cookieInvalidated: true, +currentUserInfo: LoggedOutUserInfo, +sessionID?: null | string, - +cookie?: string, + +cookie?: null | string, }; +export const genericCookieInvalidation: ClientSessionChange = { + cookieInvalidated: true, + currentUserInfo: { anonymous: true }, + sessionID: null, + cookie: null, +}; + export type PreRequestUserKeyserverSessionInfo = { +cookie: ?string, +sessionID: ?string,