diff --git a/lib/selectors/account-selectors.js b/lib/selectors/account-selectors.js --- a/lib/selectors/account-selectors.js +++ b/lib/selectors/account-selectors.js @@ -2,6 +2,7 @@ import { createSelector } from 'reselect'; +import { cookieSelector } from './keyserver-selectors.js'; import { currentCalendarQuery } from './nav-selectors.js'; import type { LogInExtraInfo } from '../types/account-types.js'; import type { CalendarQuery } from '../types/entry-types.js'; @@ -33,7 +34,7 @@ const preRequestUserStateSelector: (state: AppState) => PreRequestUserState = createSelector( (state: AppState) => state.currentUserInfo, - (state: AppState) => state.cookie, + cookieSelector, (state: AppState) => state.sessionID, ( currentUserInfo: ?CurrentUserInfo, diff --git a/lib/selectors/keyserver-selectors.js b/lib/selectors/keyserver-selectors.js new file mode 100644 --- /dev/null +++ b/lib/selectors/keyserver-selectors.js @@ -0,0 +1,10 @@ +// @flow + +import type { AppState } from '../types/redux-types.js'; +import { ashoatKeyserverID } from '../utils/validation-utils.js'; + +const cookieSelector: (state: AppState) => ?string = (state: AppState) => + state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.cookie ?? + state.cookie; + +export { cookieSelector }; diff --git a/lib/selectors/server-calls.js b/lib/selectors/server-calls.js --- a/lib/selectors/server-calls.js +++ b/lib/selectors/server-calls.js @@ -2,6 +2,7 @@ import { createSelector } from 'reselect'; +import { cookieSelector } from './keyserver-selectors.js'; import type { LastCommunicatedPlatformDetails } from '../types/device-types.js'; import type { AppState } from '../types/redux-types.js'; import { type ConnectionStatus } from '../types/socket-types.js'; @@ -18,7 +19,7 @@ const serverCallStateSelector: (state: AppState) => ServerCallState = createSelector( - (state: AppState) => state.cookie, + cookieSelector, (state: AppState) => state.urlPrefix, (state: AppState) => state.sessionID, (state: AppState) => state.currentUserInfo, 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 @@ -1,5 +1,6 @@ // @flow +import { cookieSelector } from '../selectors/keyserver-selectors.js'; import { logInActionSources, type LogInActionSource, @@ -30,7 +31,7 @@ (actionCurrentUserInfo && actionCurrentUserInfo.anonymous)) && preRequestUserState && (preRequestUserState.currentUserInfo?.id !== currentCurrentUserInfo.id || - preRequestUserState.cookie !== currentReduxState.cookie || + preRequestUserState.cookie !== cookieSelector(currentReduxState) || preRequestUserState.sessionID !== currentReduxState.sessionID) ); } diff --git a/native/account/logged-out-modal.react.js b/native/account/logged-out-modal.react.js --- a/native/account/logged-out-modal.react.js +++ b/native/account/logged-out-modal.react.js @@ -18,6 +18,7 @@ import { useDispatch } from 'react-redux'; import { resetUserStateActionType } from 'lib/actions/user-actions.js'; +import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { logInActionSources } from 'lib/types/account-types.js'; import type { Dispatch } from 'lib/types/redux-types.js'; @@ -774,7 +775,7 @@ state => !!(state._persist && state._persist.rehydrated && navContext), ); const persistedStateLoaded = usePersistedStateLoaded(); - const cookie = useSelector(state => state.cookie); + const cookie = useSelector(cookieSelector); const urlPrefix = useSelector(state => state.urlPrefix); const loggedIn = useSelector(isLoggedIn); const dimensions = useSelector(derivedDimensionsInfoSelector); diff --git a/native/data/sqlite-data-handler.js b/native/data/sqlite-data-handler.js --- a/native/data/sqlite-data-handler.js +++ b/native/data/sqlite-data-handler.js @@ -7,6 +7,7 @@ import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; import { MediaCacheContext } from 'lib/components/media-cache-provider.react.js'; import { convertClientDBReportToClientReportCreationRequest } from 'lib/ops/report-store-ops.js'; +import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { logInActionSources, @@ -32,7 +33,7 @@ const rehydrateConcluded = useSelector( state => !!(state._persist && state._persist.rehydrated), ); - const cookie = useSelector(state => state.cookie); + const cookie = useSelector(cookieSelector); const urlPrefix = useSelector(state => state.urlPrefix); const staffCanSee = useStaffCanSee(); const { staffUserHasBeenLoggedIn } = React.useContext(StaffContext); diff --git a/native/navigation/navigation-handler.react.js b/native/navigation/navigation-handler.react.js --- a/native/navigation/navigation-handler.react.js +++ b/native/navigation/navigation-handler.react.js @@ -2,6 +2,7 @@ import * as React from 'react'; +import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { logInActionType, logOutActionType } from './action-types.js'; @@ -14,7 +15,6 @@ import ThreadScreenTracker from './thread-screen-tracker.react.js'; import DevTools from '../redux/dev-tools.react.js'; import { useSelector } from '../redux/redux-utils.js'; -import type { AppState } from '../redux/state-types.js'; import { usePersistedStateLoaded } from '../selectors/app-state-selectors.js'; const NavigationHandler: React.ComponentType<{}> = React.memo<{}>( @@ -61,9 +61,9 @@ const { dispatch } = props; const hasCurrentUserInfo = useSelector(isLoggedIn); - const hasUserCookie = useSelector( - (state: AppState) => !!(state.cookie && state.cookie.startsWith('user=')), - ); + + const cookie = useSelector(cookieSelector); + const hasUserCookie = !!(cookie && cookie.startsWith('user=')); const loggedIn = hasCurrentUserInfo && hasUserCookie; const navLoggedIn = useIsAppLoggedIn(); diff --git a/native/selectors/socket-selectors.js b/native/selectors/socket-selectors.js --- a/native/selectors/socket-selectors.js +++ b/native/selectors/socket-selectors.js @@ -2,6 +2,7 @@ import { createSelector } from 'reselect'; +import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import { getClientResponsesSelector, sessionStateFuncSelector, @@ -30,14 +31,14 @@ // be reopened. By including the cookie here, whenever the cookie changes this // function will change, which tells the Socket component to restart the // connection. - (state: AppState) => state.cookie, + cookieSelector, createOpenSocketFunction, ); const sessionIdentificationSelector: ( state: AppState, ) => SessionIdentification = createSelector( - (state: AppState) => state.cookie, + cookieSelector, (cookie: ?string): SessionIdentification => ({ cookie }), ); diff --git a/native/socket.react.js b/native/socket.react.js --- a/native/socket.react.js +++ b/native/socket.react.js @@ -6,6 +6,7 @@ import { logOut, logOutActionTypes } from 'lib/actions/user-actions.js'; import { preRequestUserStateSelector } from 'lib/selectors/account-selectors.js'; +import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { accountHasPassword } from 'lib/shared/account-utils.js'; import Socket, { type BaseSocketProps } from 'lib/socket/socket.react.js'; @@ -37,7 +38,7 @@ const inputState = React.useContext(InputStateContext); const navContext = React.useContext(NavContext); - const cookie = useSelector(state => state.cookie); + const cookie = useSelector(cookieSelector); const urlPrefix = useSelector(state => state.urlPrefix); const connection = useSelector(state => state.connection); const frozen = useSelector(state => state.frozen); diff --git a/web/socket.react.js b/web/socket.react.js --- a/web/socket.react.js +++ b/web/socket.react.js @@ -5,6 +5,7 @@ import { logOut } from 'lib/actions/user-actions.js'; import { preRequestUserStateSelector } from 'lib/selectors/account-selectors.js'; +import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import Socket, { type BaseSocketProps } from 'lib/socket/socket.react.js'; import { useServerCall, @@ -25,7 +26,7 @@ const WebSocket: React.ComponentType = React.memo(function WebSocket(props) { - const cookie = useSelector(state => state.cookie); + const cookie = useSelector(cookieSelector); const urlPrefix = useSelector(state => state.urlPrefix); const connection = useSelector(state => state.connection); const active = useSelector(