Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

6.2 KB
Raw Blame
'use client';

import { toast } from 'sonner';
import { useTranslations } from 'next-intl';
import type { InstanceAuthSettings } from '@/lib/api/endpoints/god';
import SettingsCard from '@/components/common/page/SettingsCard';
import SettingsSection from '@/components/common/page/SettingsSection';
import SettingsRow from '@/components/common/page/SettingsRow';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Switch } from '@/components/ui/switch';
import GodSectionPage from '../GodSectionPage';
import RegistrationModePicker from './RegistrationModePicker';
import { MAX_INVITE_TTL_DAYS, useGodPolicyForm } from '../../hooks/useGodPolicyForm';

// The registration policy and the sign-in options. The provider credentials live
// under Integrations (Email provider, Auth provider).
export default function GodAuthenticationForm({
  authSettings,
}: {
  authSettings: InstanceAuthSettings;
}) {
  const t = useTranslations('god.authentication');
  const tCommon = useTranslations('common');
  const policy = useGodPolicyForm(authSettings);

  // The options that send mail need a configured provider; the API rejects them
  // without one.
  const needsProvider = !authSettings.hasEmailProvider;
  // Turning the password form off needs another way in, or nobody could sign in at
  // all. The API rejects it, and the switch says why before anyone tries.
  const needsSso = !authSettings.hasSsoProvider;

  async function save() {
    try {
      await policy.save();
      toast.success(t('saved'));
    } catch {
      // The failure already surfaced through the global mutation error toast.
    }
  }

  return (
    <GodSectionPage
      slug="authentication"
      actions={
        <Button
          size="sm"
          onClick={() => void save()}
          disabled={
            !policy.dirty ||
            !policy.inviteTtlDaysValid ||
            !policy.allowedEmailDomainsValid ||
            policy.saving
          }
        >
          {policy.saving ? tCommon('saving') : tCommon('save')}
        </Button>
      }
    >
      <div className="space-y-10">
        <SettingsSection title={t('registration')} description={t('registrationHint')}>
          <SettingsCard className="divide-y divide-border/60">
            <RegistrationModePicker
              value={policy.registration}
              disabled={policy.saving}
              onChange={policy.setRegistration}
              passwordless={!policy.emailPassword}
            />
            <SettingsRow
              title={t('allowedEmailDomains')}
              description={t('allowedEmailDomainsHint')}
              note={policy.allowedEmailDomainsValid ? undefined : t('allowedEmailDomainsInvalid')}
              control={
                <Input
                  className="w-40 shrink-0 sm:w-72"
                  placeholder="example.com"
                  aria-label={t('allowedEmailDomains')}
                  aria-invalid={!policy.allowedEmailDomainsValid}
                  value={policy.allowedEmailDomains}
                  disabled={policy.saving}
                  onChange={(e) => policy.setAllowedEmailDomains(e.target.value)}
                />
              }
            />
            <SettingsRow
              title={t('inviteTtlDays')}
              description={t('inviteTtlDaysHint')}
              note={
                policy.inviteTtlDaysValid
                  ? undefined
                  : t('inviteTtlDaysInvalid', { max: MAX_INVITE_TTL_DAYS })
              }
              control={
                <Input
                  type="number"
                  min={0}
                  max={MAX_INVITE_TTL_DAYS}
                  step={1}
                  inputMode="numeric"
                  className="w-24"
                  aria-label={t('inviteTtlDays')}
                  aria-invalid={!policy.inviteTtlDaysValid}
                  value={policy.inviteTtlDays}
                  disabled={policy.saving}
                  onChange={(e) => policy.setInviteTtlDays(e.target.value)}
                />
              }
            />
          </SettingsCard>
          {!policy.emailPassword && (
            <p className="text-xs text-muted-foreground">{t('registrationPasswordlessNote')}</p>
          )}
        </SettingsSection>

        <SettingsSection title={t('signIn')} description={t('signInHint')}>
          <SettingsCard className="divide-y divide-border/60">
            <SettingsRow
              title={t('emailPassword')}
              description={t('emailPasswordHint')}
              note={needsSso ? t('needsSso') : undefined}
              control={
                <Switch
                  checked={policy.emailPassword}
                  disabled={policy.saving || (needsSso && policy.emailPassword)}
                  onCheckedChange={policy.setEmailPassword}
                />
              }
            />
            <SettingsRow
              title={t('requireVerification')}
              description={t('requireVerificationHint')}
              note={needsProvider ? t('needsProvider') : undefined}
              control={
                <Switch
                  checked={policy.requireEmailVerification}
                  disabled={policy.saving || needsProvider}
                  onCheckedChange={policy.setRequireEmailVerification}
                />
              }
            />
            <SettingsRow
              title={t('magicLink')}
              description={t('magicLinkHint')}
              note={needsProvider ? t('needsProvider') : undefined}
              control={
                <Switch
                  checked={policy.magicLink}
                  disabled={policy.saving || needsProvider}
                  onCheckedChange={policy.setMagicLink}
                />
              }
            />
            <SettingsRow
              title={t('trustProviderEmails')}
              description={t('trustProviderEmailsHint')}
              note={needsSso ? t('needsSso') : undefined}
              control={
                <Switch
                  checked={policy.trustProviderEmails}
                  disabled={policy.saving || needsSso}
                  onCheckedChange={policy.setTrustProviderEmails}
                />
              }
            />
          </SettingsCard>
        </SettingsSection>
      </div>
    </GodSectionPage>
  );
}