fix: On Sign Out > Sign In, States are not loaded properly (#3041)

* fix: On Sign Out > Sign In, States are not loaded properly

* draft: reset on logout

* chore: lint

* chore: lint

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Andrey Kud
2023-12-21 17:44:17 -03:00
committed by GitHub
parent ce6214d6ef
commit 0ae0bf73fd

View File

@ -3,6 +3,7 @@ import { useApolloClient } from '@apollo/client';
import { import {
snapshot_UNSTABLE, snapshot_UNSTABLE,
useGotoRecoilSnapshot, useGotoRecoilSnapshot,
useRecoilCallback,
useRecoilState, useRecoilState,
useSetRecoilState, useSetRecoilState,
} from 'recoil'; } from 'recoil';
@ -10,6 +11,13 @@ import {
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState'; import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState'; import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { isVerifyPendingState } from '@/auth/states/isVerifyPendingState'; import { isVerifyPendingState } from '@/auth/states/isVerifyPendingState';
import { authProvidersState } from '@/client-config/states/authProvidersState';
import { billingState } from '@/client-config/states/billingState';
import { isDebugModeState } from '@/client-config/states/isDebugModeState';
import { isSignInPrefilledState } from '@/client-config/states/isSignInPrefilledState';
import { supportChatState } from '@/client-config/states/supportChatState';
import { telemetryState } from '@/client-config/states/telemetryState';
import { iconsState } from '@/ui/display/icon/states/iconsState';
import { ColorScheme } from '@/workspace-member/types/WorkspaceMember'; import { ColorScheme } from '@/workspace-member/types/WorkspaceMember';
import { REACT_APP_SERVER_AUTH_URL } from '~/config'; import { REACT_APP_SERVER_AUTH_URL } from '~/config';
import { import {
@ -125,14 +133,39 @@ export const useAuth = () => {
[handleChallenge, handleVerify, setIsVerifyPendingState], [handleChallenge, handleVerify, setIsVerifyPendingState],
); );
const handleSignOut = useCallback(() => { const handleSignOut = useRecoilCallback(
goToRecoilSnapshot(snapshot_UNSTABLE()); ({ snapshot }) =>
setTokenPair(null); async () => {
setCurrentUser(null); const emptySnapshot = snapshot_UNSTABLE();
client.clearStore().then(() => { const iconsValue = snapshot.getLoadable(iconsState).getValue();
sessionStorage.clear(); const authProvidersValue = snapshot
}); .getLoadable(authProvidersState)
}, [goToRecoilSnapshot, setTokenPair, setCurrentUser, client]); .getValue();
const billing = snapshot.getLoadable(billingState).getValue();
const isSignInPrefilled = snapshot
.getLoadable(isSignInPrefilledState)
.getValue();
const supportChat = snapshot.getLoadable(supportChatState).getValue();
const telemetry = snapshot.getLoadable(telemetryState).getValue();
const isDebugMode = snapshot.getLoadable(isDebugModeState).getValue();
const initialSnapshot = emptySnapshot.map(({ set }) => {
set(iconsState, iconsValue);
set(authProvidersState, authProvidersValue);
set(billingState, billing);
set(isSignInPrefilledState, isSignInPrefilled);
set(supportChatState, supportChat);
set(telemetryState, telemetry);
set(isDebugModeState, isDebugMode);
});
goToRecoilSnapshot(initialSnapshot);
await client.clearStore();
sessionStorage.clear();
},
[client, goToRecoilSnapshot],
);
const handleCredentialsSignUp = useCallback( const handleCredentialsSignUp = useCallback(
async (email: string, password: string, workspaceInviteHash?: string) => { async (email: string, password: string, workspaceInviteHash?: string) => {