To implement Time-Based One-Time Passwords (TOTP), instantiate a new OTPAuth.TOTP() object. You can provide an issuer, label, algorithm, digits, period, and a secret (as a base32 string or OTPAuth.Secret instance).
Common methods include:
.generate(): Returns the current token as a string..validate({ token, window }): Validates a token against a search window to account for clock drift. Returns the token delta or null if invalid..counter(): Returns the current counter value..remaining(): Returns milliseconds until the next token change..toString(): Converts the TOTP object to a Google Authenticator compatible URI string.
import * as OTPAuth from "otpauth";
let totp = new OTPAuth.TOTP({
issuer: "ACME",
label: "Alice",
algorithm: "SHA1",
digits: 6,
period: 30,
secret: "US3WHSG7X5KAPV27VANWKQHF3SH3HULL",
});
let token = totp.generate();
let delta = totp.validate({ token, window: 1 });
let uri = totp.toString();