Finn24.09Original post
Web Developer
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:
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
Summary
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.