Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Documentationbreadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow jslibbreadcrumb arrow totpbreadcrumb arrow TOTP.verify(code, [timeStep])
Open source

TOTP.verify(code, [timeStep])

Verifies a TOTP code against the current time.

ParameterTypeDescription
codestringThe TOTP code to verify
timeStep (optional)numberTime step in seconds (default: 30)

Returns

TypeDescription
Promisetrue if the code is valid, false otherwise

Example

JavaScript
import { TOTP } from 'https://jslib.k6.io/totp/1.0.0/index.js';

export default async function () {
  const totp = new TOTP('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', 6);

  // Generate and verify a code
  const code = await totp.gen();
  const isValid = await totp.verify(code);

  if (isValid) {
    console.log('Code is valid!');
  } else {
    console.log('Code is invalid!');
  }
}