fix: prevent logout 401 and login double-attempt caused by LWW layout push race
resetUserStores() was abusing populateFromReady('', [], [], []) to clear the
space store. Its LWW timestamp logic fired an async pushLayoutToOrigin when
_layoutUpdatedAt > 0, which read a null token from localStorage (already
removed on logout, not yet set on login). The 401 response triggered
handleUnauthorized(), deleting the freshly-stored login token and forcing a
full page reload — requiring users to log in twice.
Replace with a proper reset() method that synchronously sets all state to
initial values with no LWW comparison or API side effects.
This commit is contained in:
@@ -31,7 +31,7 @@ interface AuthState {
|
|||||||
function resetUserStores() {
|
function resetUserStores() {
|
||||||
clearSelfIds();
|
clearSelfIds();
|
||||||
useChatStore.getState().clearAllMessages();
|
useChatStore.getState().clearAllMessages();
|
||||||
useSpaceStore.getState().populateFromReady('', [], [], []);
|
useSpaceStore.getState().reset();
|
||||||
useSocialStore.getState().reset();
|
useSocialStore.getState().reset();
|
||||||
useVoiceStore.getState().resetSession();
|
useVoiceStore.getState().resetSession();
|
||||||
useInstanceStore.getState().reset();
|
useInstanceStore.getState().reset();
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ interface SpaceState {
|
|||||||
removeInstanceSpaces: (origin: string) => void;
|
removeInstanceSpaces: (origin: string) => void;
|
||||||
transferOwnership: (spaceId: string, newOwnerId: string) => Promise<void>;
|
transferOwnership: (spaceId: string, newOwnerId: string) => Promise<void>;
|
||||||
findExistingDmForUser: (targetUser: { id: string; homeUserId?: string | null }) => { dm: DmChannel; origin: string } | null;
|
findExistingDmForUser: (targetUser: { id: string; homeUserId?: string | null }) => { dm: DmChannel; origin: string } | null;
|
||||||
|
reset: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,6 +130,28 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
|||||||
categoryOriginMap: new Map(),
|
categoryOriginMap: new Map(),
|
||||||
_layoutUpdatedAt: 0,
|
_layoutUpdatedAt: 0,
|
||||||
|
|
||||||
|
reset: () => {
|
||||||
|
_myUserIdByOrigin.clear();
|
||||||
|
set({
|
||||||
|
spaces: [],
|
||||||
|
currentSpaceId: null,
|
||||||
|
channels: [],
|
||||||
|
categories: [],
|
||||||
|
members: [],
|
||||||
|
roles: [],
|
||||||
|
folders: [],
|
||||||
|
spaceLayout: null,
|
||||||
|
dmChannels: [],
|
||||||
|
channelToSpaceMap: new Map(),
|
||||||
|
channelLastMessageIds: new Map(),
|
||||||
|
spacePermissions: new Map(),
|
||||||
|
channelPermissions: new Map(),
|
||||||
|
channelOriginMap: new Map(),
|
||||||
|
categoryOriginMap: new Map(),
|
||||||
|
_layoutUpdatedAt: 0,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
setSpaces: (spaces) => set({ spaces }),
|
setSpaces: (spaces) => set({ spaces }),
|
||||||
setCurrentSpace: (spaceId) => set({ currentSpaceId: spaceId }),
|
setCurrentSpace: (spaceId) => set({ currentSpaceId: spaceId }),
|
||||||
setChannels: (channels) => set({ channels }),
|
setChannels: (channels) => set({ channels }),
|
||||||
|
|||||||
Reference in New Issue
Block a user