Skip to content

The open alternative to Salesforce, designed for AI.

3.9 KB
Raw Blame
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { authProvidersState } from '@/client-config/states/authProvidersState';
import { getToastOptionsFromError } from '@/error-handler/utils/getToastOptionsFromError';
import { SettingsOptionCardContentSwitch } from '@/settings/components/SettingsOptions/SettingsOptionCardContentSwitch';
import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';

import { useMutation } from '@apollo/client/react';
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { capitalize } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { IconGoogle, IconMicrosoft, IconPassword } from 'twenty-ui/icon';
import { Card } from 'twenty-ui/primitives/surfaces';
import { themeCssVariables } from 'twenty-ui/theme';
import {
  type AuthProviders,
  UpdateWorkspaceDocument,
} from '~/generated-metadata/graphql';

const StyledSettingsSecurityOptionsList = styled.div`
  display: flex;
  flex-direction: column;
  gap: ${themeCssVariables.spacing[4]};
`;

export const SettingsSecurityAuthBypassOptionsList = () => {
  const { t } = useLingui();

  const { enqueueToast } = useToast();
  const authProviders = useAtomStateValue(authProvidersState);

  const [currentWorkspace, setCurrentWorkspace] = useAtomState(
    currentWorkspaceState,
  );

  const [updateWorkspace] = useMutation(UpdateWorkspaceDocument);

  const isValidAuthProvider = (
    key: string,
  ): key is Exclude<keyof typeof currentWorkspace, '__typename'> => {
    if (!currentWorkspace) return false;
    return Reflect.has(currentWorkspace, key);
  };

  const toggleAuthBypassMethod = async (
    authProvider: keyof Omit<AuthProviders, '__typename' | 'magicLink' | 'sso'>,
  ) => {
    if (!currentWorkspace?.id) {
      throw new Error(t`User is not logged in`);
    }

    const key = `is${capitalize(authProvider)}AuthBypassEnabled`;

    if (!isValidAuthProvider(key)) {
      throw new Error(t`Invalid auth bypass provider`);
    }

    setCurrentWorkspace({
      ...currentWorkspace,
      [key]: !currentWorkspace[key],
    });

    updateWorkspace({
      variables: {
        input: {
          [key]: !currentWorkspace[key],
        },
      },
    }).catch((err) => {
      setCurrentWorkspace({
        ...currentWorkspace,
        [key]: currentWorkspace[key],
      });
      enqueueToast(getToastOptionsFromError({ error: err }));
    });
  };

  if (!currentWorkspace) {
    return null;
  }

  return (
    <StyledSettingsSecurityOptionsList>
      <Card.Root>
        {authProviders.google === true && (
          <SettingsOptionCardContentSwitch
            Icon={IconGoogle}
            title={t`Google`}
            description={t`Allow Google-based login for users with SSO bypass permissions.`}
            checked={currentWorkspace.isGoogleAuthBypassEnabled}
            divider
            onChange={() => toggleAuthBypassMethod('google')}
          />
        )}
        {authProviders.microsoft === true && (
          <SettingsOptionCardContentSwitch
            Icon={IconMicrosoft}
            title={t`Microsoft`}
            description={t`Allow Microsoft-based login for users with SSO bypass permissions.`}
            checked={currentWorkspace.isMicrosoftAuthBypassEnabled}
            divider
            onChange={() => toggleAuthBypassMethod('microsoft')}
          />
        )}
        {authProviders.password && (
          <SettingsOptionCardContentSwitch
            Icon={IconPassword}
            title={t`Password`}
            description={t`Allow email & password login for SSO bypass users.`}
            checked={currentWorkspace.isPasswordAuthBypassEnabled}
            onChange={() => toggleAuthBypassMethod('password')}
          />
        )}
      </Card.Root>
    </StyledSettingsSecurityOptionsList>
  );
};