diff --git a/packages/twenty-front/src/modules/auth/hooks/useAuth.ts b/packages/twenty-front/src/modules/auth/hooks/useAuth.ts index b23c2360fa..66e22df49c 100644 --- a/packages/twenty-front/src/modules/auth/hooks/useAuth.ts +++ b/packages/twenty-front/src/modules/auth/hooks/useAuth.ts @@ -272,6 +272,7 @@ export const useAuth = () => { handleSetLoginToken(loginToken); navigate(AppPath.SignInUp); setSignInUpStep(SignInUpStep.TwoFactorAuthenticationProvision); + return; } if ( @@ -283,7 +284,9 @@ export const useAuth = () => { handleSetLoginToken(loginToken); navigate(AppPath.SignInUp); setSignInUpStep(SignInUpStep.TwoFactorAuthenticationVerification); + return; } + throw error; } }, [ diff --git a/packages/twenty-front/src/modules/auth/hooks/useImpersonationSession.ts b/packages/twenty-front/src/modules/auth/hooks/useImpersonationSession.ts index 3a5c5a8b8d..17dfd9cff3 100644 --- a/packages/twenty-front/src/modules/auth/hooks/useImpersonationSession.ts +++ b/packages/twenty-front/src/modules/auth/hooks/useImpersonationSession.ts @@ -1,5 +1,5 @@ -import { useCallback } from 'react'; import { useStore } from 'jotai'; +import { useCallback } from 'react'; import { useAuth } from '@/auth/hooks/useAuth'; import { tokenPairState } from '@/auth/states/tokenPairState'; @@ -40,7 +40,13 @@ export const useImpersonationSession = () => { ); } - await getAuthTokensFromLoginToken(loginToken); + try { + await getAuthTokensFromLoginToken(loginToken); + } catch (error) { + sessionStorage.removeItem(IMPERSONATION_SESSION_KEY); + throw error; + } + reloadWithSession(targetPath); }, [store, getAuthTokensFromLoginToken], diff --git a/packages/twenty-front/src/modules/settings/admin-panel/hooks/useHandleImpersonate.ts b/packages/twenty-front/src/modules/settings/admin-panel/hooks/useHandleImpersonate.ts index 92857e74c7..eee9fd7808 100644 --- a/packages/twenty-front/src/modules/settings/admin-panel/hooks/useHandleImpersonate.ts +++ b/packages/twenty-front/src/modules/settings/admin-panel/hooks/useHandleImpersonate.ts @@ -1,10 +1,13 @@ import { useState } from 'react'; import { useMutation } from '@apollo/client/react'; +import { t } from '@lingui/core/macro'; import { AppPath } from 'twenty-shared/types'; +import { isDefined } from 'twenty-shared/utils'; -import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState'; import { useImpersonationSession } from '@/auth/hooks/useImpersonationSession'; +import { currentUserState } from '@/auth/states/currentUserState'; +import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState'; import { useRedirectToWorkspaceDomain } from '@/domain-manager/hooks/useRedirectToWorkspaceDomain'; import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar'; import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue'; @@ -12,6 +15,7 @@ import { ImpersonateDocument } from '~/generated-metadata/graphql'; import { getWorkspaceUrl } from '~/utils/getWorkspaceUrl'; export const useHandleImpersonate = () => { + const currentUser = useAtomStateValue(currentUserState); const currentWorkspace = useAtomStateValue(currentWorkspaceState); const { enqueueErrorSnackBar } = useSnackBar(); const { startImpersonating } = useImpersonationSession(); @@ -22,6 +26,14 @@ export const useHandleImpersonate = () => { ); const handleImpersonate = async (userId: string, workspaceId: string) => { + if (!isDefined(currentUser?.id) || userId === currentUser.id) { + enqueueErrorSnackBar({ + message: t`You cannot impersonate your own account`, + }); + + return; + } + setImpersonatingUserId(userId); await impersonate({ diff --git a/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminUserDetail.tsx b/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminUserDetail.tsx index 0b4837560d..467339567f 100644 --- a/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminUserDetail.tsx +++ b/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminUserDetail.tsx @@ -166,7 +166,8 @@ export const SettingsAdminUserDetail = () => { /> {currentUser?.canImpersonate && activeWorkspace && - isDefined(user) && ( + isDefined(user) && + user.id !== currentUser.id && (