Back

Unable to activate MFA for Users - 401 (Unauthorized)

  • 0
  • Self Hosted
  • Auth
  • Web
Finn24.09
5 Jan, 2025, 23:21

Good evening all,

today I wanted to add MFA using TOTP but failed on activating it as the last step always returns: https://myappwriteinstacne.example.com/v1/account/mfa/authenticators/totp 401 (Unauthorized)

The user is correctly signed in and I can recieve account informations using account.get().

My code looks like this:

TypeScript
import { useState, useEffect } from "react";
import { account, avatars } from "./AppwriteConfig";

function Enable2fa() {
  const [user, setUser] = useState<any>("");
  const [qrCodeUrl, setQrCodeUrl] = useState<string>("");
  const [totpSecret, setTotpSecret] = useState<string>("");
  const [totpCode, setTOTPCode] = useState<string>("");

  useEffect(() => {
    const validateSession = async () => {
      try {
        const getaccount = await account.get();
        setUser(getaccount);
      } catch {
        window.location.replace("/");
        return;
      }
    };

    const create2faTotp = async () => {
      try {
        const { secret, uri } = await (account as any).createMfaAuthenticator(
          "totp" // type
        );

        const result = avatars.getQR(
          uri, // url
          300, // size
          0, // margin
          false // download
        );

        setQrCodeUrl(result);
        setTotpSecret(secret);
      } catch (error) {
        console.log(error);
      }
    };

    validateSession();
    create2faTotp();
  }, []);

  const handleActivation = async () => {
    try {
      await (account as any).updateMfaAuthenticator(
        "totp", // type
        totpCode // otp
      );
      //await account.updateMFA(true);
    } catch {
      setMessageType("error");
      setMessage(
        "Failed to enable 2FA. Please verify you're logged in correctly, reload the webpage/app, or get in touch with the application owner."
      );
    }
  };

Does anyone have an Idea on what im doing wrong here?

Many thanks and best regards Finn

TL;DR
Unable to activate MFA using TOTP, issue with 401 (Unauthorized) error when attempting last step in code. User successfully signed in and can receive account information. Issue might be with the updateMfaAuthenticator method.
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