Back

Error update MFA

  • 0
  • Self Hosted
  • Auth
  • Web
losfroger
17 Jul, 2024, 06:34

I was trying to follow the docs to activate MFA: https://appwrite.io/docs/products/auth/mfa

But when I called the account.updateMFA(true) I got the following exception:

TypeScript
AppwriteException: Invalid document structure: Attribute "factors" must be an array

idk if there's a missing step in the docs or I'm doing it incorrectly

This is the code I'm using (it's vue 3, but this is just JS):

TL;DR
Developers are encountering an error with the `updateMFA(true)` call when implementing MFA. The exception thrown mentions that the "factors" attribute must be an array, and this issue arises when trying to update MFA. The issue may lie in the format of the data being sent during the updateMFA call. Check the structure being passed to this function to ensure it meets the requirements. You may need to adjust the data or consult the docs for the correct format.
losfroger
17 Jul, 2024, 06:35

Here's the Vue code I'm using:

TypeScript
// STEP 1
const recoveryCodes = ref<Models.MfaRecoveryCodes | undefined>()
async function startMfaRecoveryCodes() {
  try {
    step.value = MFA_STEPS.RECOVERY_CODES
    showMfaModal.value = true

    const res = await appwriteStore.account.createMfaRecoveryCodes()
    recoveryCodes.value = res

    console.log('Step 1', res)

  } catch (error) {
    console.error(error)
    if (error instanceof AppwriteException) {
      alert(error.message)
      return
    }
    alert('Error loading thing')
  }
}

//STEP 2

const mfaUri = ref<string | undefined>()
const otp = ref('')
async function makeMfaAuth() {
  try {
    const res = await appwriteStore.account.createMfaAuthenticator(AuthenticatorType.Totp)
    mfaUri.value = res.uri

    console.log('Step 2', res)

    step.value = MFA_STEPS.VERIFY_MFA_FACTOR
  } catch (error) {
    console.error(error)
    if (error instanceof AppwriteException) {
      alert(error.message)
      return
    }
    alert('Error loading MFA')
  }
}

// STEP 3
async function enableOtp() {
  try {
    const resMfaAuth = await appwriteStore.account.updateMfaAuthenticator(
      AuthenticatorType.Totp,
      otp.value,
    )

    console.log('Step 3: MFA', resMfaAuth)

    const mfaFactors = await appwriteStore.account.listMfaFactors()
    console.log(mfaFactors)

    // THIS GENERATES ERROR
    const resUpdate = await appwriteStore.account.updateMFA(true)

    console.log('Step 3: Update', resUpdate)

    alert('MFA activated')
    authStore.reloadUserAccount()

    showMfaModal.value = false
  } catch (error) {
    console.error(error)
    if (error instanceof AppwriteException) {
      alert(error.message)
      return
    }
    alert('Error Validating OTP')
  }
}
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more