import { createFormControl, createFormGroup } from "solid-forms"; import { QRCodeSVG } from "solid-qr-code"; import { Trans, useLingui } from "@lingui-solid/solid/macro"; import { styled } from "styled-system/jsx"; import { Column, Dialog, DialogProps, Form2, Text } from "@revolt/ui"; import { useModals } from ".."; import { Modals } from "../types"; /** * Wrapper element for the raw TOTP code */ const Code = styled("code", { base: { userSelect: "all", }, }); /** * Wrapper element for the QR code */ const Qr = styled("div", { base: { borderRadius: "4px", background: "white", width: "140px", height: "140px", display: "grid", placeItems: "center", }, }); /** * Modal to display QR code and secret key for MFA and accept the correct code */ export function MFAEnableTOTPModal( props: DialogProps & Modals & { type: "mfa_enable_totp" }, ) { const { t } = useLingui(); const { showError } = useModals(); const group = createFormGroup({ code: createFormControl("", { required: true }), }); /** * Generate OTP URI */ const uri = () => `otpauth://totp/Stoat:${props.identifier}?secret=${props.secret}&issuer=Stoat`; async function onSubmit() { try { const code = group.controls.code.value.trim().replace(/\s/g, ""); await props.callback(code); props.onClose(); } catch (error) { showError(error); } } const submit = Form2.useSubmitHandler(group, onSubmit); return ( { props.callback(); props.onClose(); }} title={Enable authenticator app} actions={[ { text: Cancel, onClick() { props.callback(); }, }, { text: Continue, onClick() { onSubmit(); return false; }, isDisabled: !Form2.canSubmit(group), }, ]} isDisabled={group.isPending} >
Please scan or use the token below in your authenticator app. {props.secret}
); }