feat(auth): add MFA factors policy card

Lets a project choose which factors can complete an MFA challenge,
including the new custom factor, where Appwrite generates and verifies
the code and the app delivers it over a channel of its own.

The card stays hidden when listPolicies() omits mfa-factors, so a
self-hosted server older than 1.9.6 does not render a control whose
PATCH would 404.
This commit is contained in:
harsh mahajan committed 2026-08-17 15:36:15 +05:30
1 parent 744d6eafb5
commit 17f0fa5124
4 files changed
+95

No files matched your search

+1
View File
@@ -283,6 +283,7 @@ export enum Submit {
AuthCorporateEmailsUpdate = 'submit_auth_corporate_emails_update',
AuthSessionAlertsUpdate = 'submit_auth_session_alerts_update',
AuthMembershipPrivacyUpdate = 'submit_auth_membership_privacy_update',
AuthMfaFactorsUpdate = 'submit_auth_mfa_factors_update',
AuthMockNumbersUpdate = 'submit_auth_mock_numbers_update',
AuthInvalidateSession = 'submit_auth_invalidate_session',
SessionsLengthUpdate = 'submit_sessions_length_update',
@@ -8,6 +8,7 @@
import UpdateSessionsLimit from './updateSessionsLimit.svelte';
import PasswordPolicies from './passwordPolicies.svelte';
import PasswordStrengthPolicy from './passwordStrengthPolicy.svelte';
import UpdateMfaFactors from './updateMfaFactors.svelte';
import SessionSecurity from './sessionSecurity.svelte';
import UpdateSignupEmailSecurity from './updateSignupEmailSecurity.svelte';
import { isCloud } from '$lib/system';
@@ -25,6 +26,9 @@
dictionaryPolicy={data.passwordDictionaryPolicy}
historyPolicy={data.passwordHistoryPolicy}
personalDataPolicy={data.passwordPersonalDataPolicy} />
{#if data.mfaFactorsPolicy}
<UpdateMfaFactors project={data.project} policy={data.mfaFactorsPolicy} />
{/if}
{#if isCloud}
<UpdateSignupEmailSecurity
project={data.project}
@@ -41,6 +41,9 @@ export const load: PageLoad = async ({ depends, params }) => {
) as Partial<Record<ProjectPolicyId | EmailPolicyId, ProjectPolicy>>;
return {
mfaFactorsPolicy: policiesById[ProjectPolicyId.Mfafactors] as
| Models.PolicyMfaFactors
| undefined,
membershipPrivacyPolicy: policiesById[
ProjectPolicyId.Membershipprivacy
] as Models.PolicyMembershipPrivacy,
@@ -0,0 +1,87 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { CardGrid } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, Form } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdk } from '$lib/stores/sdk';
import { Selector } from '@appwrite.io/pink-svelte';
import type { Models } from '@appwrite.io/console';
const {
project,
policy
}: {
project: Models.Project;
policy: Models.PolicyMfaFactors;
} = $props();
let totp = $state(policy.totp);
let email = $state(policy.email);
let phone = $state(policy.phone);
let custom = $state(policy.custom);
const isSubmitDisabled = $derived(
totp === policy.totp &&
email === policy.email &&
phone === policy.phone &&
custom === policy.custom
);
async function updateMfaFactors() {
try {
await sdk.forProject(project.region, project.$id).project.updateMFAFactorsPolicy({
totp,
email,
phone,
custom
});
await invalidate(Dependencies.PROJECT);
addNotification({
type: 'success',
message: 'Updated MFA factors'
});
trackEvent(Submit.AuthMfaFactorsUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.AuthMfaFactorsUpdate);
}
}
</script>
<Form onSubmit={updateMfaFactors}>
<CardGrid>
<svelte:fragment slot="title">MFA factors</svelte:fragment>
Choose which factors your users can use to complete a multi-factor authentication challenge. Recovery
codes always remain available as a fallback.
<svelte:fragment slot="aside">
<Selector.Checkbox
id="mfaFactorTotp"
label="TOTP"
description="Time-based codes from an authenticator app"
bind:checked={totp} />
<Selector.Checkbox
id="mfaFactorEmail"
label="Email"
description="Codes sent to the user's verified email address"
bind:checked={email} />
<Selector.Checkbox
id="mfaFactorPhone"
label="Phone"
description="Codes sent to the user's verified phone number over SMS"
bind:checked={phone} />
<Selector.Checkbox
id="mfaFactorCustom"
label="Custom"
description="Appwrite generates and verifies the code, and you deliver it through your own channel"
bind:checked={custom} />
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={isSubmitDisabled} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>