| 1 | import { TOTP } from "otpauth"; |
| 2 | |
| 3 | let totp: TOTP; |
| 4 | |
| 5 | export const extractOTPSecret = (qrCodeLink: string) => { |
| 6 | return qrCodeLink.split("secret=")[1]?.split("&")[0]; |
| 7 | }; |
| 8 | |
| 9 | export const generateOTP = (otp?: string) => { |
| 10 | if (otp) { |
| 11 | totp = new TOTP({ |
| 12 | secret: otp, |
| 13 | // Increase in case of flakiness |
| 14 | period: 5, |
| 15 | }); |
| 16 | } |
| 17 | |
| 18 | if (!totp) { |
| 19 | throw new Error("No token provider, secret has not been provided."); |
| 20 | } |
| 21 | |
| 22 | return totp.generate(); |
| 23 | }; |
| 24 | |
| 25 | export const getTokenPeriodInMs = () => { |
| 26 | return totp.period * 1000; |
| 27 | }; |