prsm/packages/otp/src/code.ts
2024-08-27 18:16:34 -04:00

35 lines
1.1 KiB
TypeScript

import QRCode from "qrcode";
import Otp from ".";
// Step 1: Create a secret
const secret = Otp.createSecret();
console.log("Secret:", secret);
// Step 2: Generate the TOTP URI
const issuer = "app.example.com";
const accountName = "john.doe@example.org";
const uri = Otp.createTotpKeyUriForQrCode(issuer, accountName, secret);
console.log("TOTP URI:", uri);
// Step 3: Generate the QR code
QRCode.toDataURL(uri, (err, url) => {
if (err) {
console.error("Error generating QR code:", err);
return;
}
console.log("QR Code URL:", url);
// If you are running this in a Node.js environment, you can save the QR code as an image file
// Uncomment the following lines to save the QR code as a PNG file
// QRCode.toFile('totp-qrcode.png', uri, (err) => {
// if (err) throw err;
// console.log('QR code saved as totp-qrcode.png');
// });
// If you are running this in a browser environment, you can display the QR code in an <img> element
// document.getElementById('qrcode').src = url;
});
// Store the secret. You need to validate the secret against the TOTP generated by the user.