import { HttpService, MfaApi } from 'authsafe-ts';
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
const mfaApi = new MfaApi(httpService);await mfaApi.listMethods(): Promise<MfaMethod[]>interface MfaMethod {
id: string;
type: 'totp' | 'email';
is_primary: boolean;
created_at: string;
last_used_at?: string;
}const methods = await mfaApi.listMethods();
console.log('Configured MFA:', methods);await mfaApi.setupTotp(): Promise<{ secret: string; qr_code: string }>{
secret: "BASE32_SECRET",
qr_code: "data:image/png;base64,..." // QR code as data URL
}const { secret, qr_code } = await mfaApi.setupTotp();
// Display QR code to user
document.getElementById('qr-code').src = qr_code;
// Show secret for manual entry
console.log('Manual entry:', secret);await mfaApi.verifyTotp(code: string): Promise<{ success: boolean }>const userCode = '123456'; // From authenticator app
const result = await mfaApi.verifyTotp(userCode);
if (result.success) {
console.log('TOTP enabled successfully');
}await mfaApi.setupEmail(email: string): Promise<{ message: string }>await mfaApi.setupEmail('user@example.com');
console.log('Verification code sent to email');await mfaApi.verifyEmail(code: string): Promise<{ success: boolean }>const emailCode = '654321'; // From user's email
const result = await mfaApi.verifyEmail(emailCode);
if (result.success) {
console.log('Email MFA enabled');
}await mfaApi.removeMethod(methodId: string): Promise<{ success: boolean }>const methods = await mfaApi.listMethods();
const totpMethod = methods.find((m) => m.type === 'totp');
if (totpMethod) {
await mfaApi.removeMethod(totpMethod.id);
console.log('TOTP removed');
}import { OAuthService, HttpService, MfaApi } from 'authsafe-ts';
// Initialize services
const oauthService = new OAuthService({ clientId, redirectUri });
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
const mfaApi = new MfaApi(httpService);
// Check existing MFA methods
const methods = await mfaApi.listMethods();
if (methods.length === 0) {
// No MFA configured - setup TOTP
const { qr_code, secret } = await mfaApi.setupTotp();
// Display QR code to user
showQRCode(qr_code);
// User scans and enters code
const code = await promptUserForCode();
const result = await mfaApi.verifyTotp(code);
if (result.success) {
console.log('TOTP enabled successfully!');
}
} else {
console.log('MFA already configured:', methods);
}const methods = await mfaApi.listMethods();
for (const method of methods) {
console.log(`Removing ${method.type} MFA...`);
await mfaApi.removeMethod(method.id);
}
console.log('All MFA methods removed');