Skip to content
Gerald Yeo edited this page Aug 25, 2019 · 6 revisions

Table of Contents


Behaviour Changes

Compiled with Babel

Due to a change in how babel treats import and requires, currently, it you need to use require, you'll need to do const otplib = require('otplib').default; instead.

Some compatibility files has been included, thus, you can use const optlib = require('otplib/compat'); if you want to skip the "default" part. We would recommend that you use import otplib from 'otplib'; instead.

Function and Classes

All classes are now wrappers over their corresponding functions. Thus, if you prefer using functions over classes, then you can look those in the core folder instead.

Setting Options

Previously, if you want to set custom options for a class, you would do the following:

// set
authenticator.options({
  step: 60
});

// no get method

However, in v4, options are now a setter and getter methods. Thus, you would assign values are retrieve values as how you would an object. i.e.

// set
authenticator.options = {
   step: 60
}

// get
const opt = authenticator.options;

Directory changes

Previously, in order to include classes, you would have had to import from otplib/lib/classes/.... The build process now puts all these into the top level, which means that you will import from otplib/classes/... instead.

Keyuri (Google Authenticator)

The keyuri method now returns non-encoded URIs. So if you need to encode it, you will have to run it through encodeURIComponent manually.

v2 compat

The v2 compatibility layer was introduced with the code change from v2 to v3. We've kept it, but behaviour wise there may be some small differences depending on the classes. We've customarily gone back and ensured backward compatibility, but it's not guaranteed. Do update to v4 API or at the very least, v3 API.


Deprecations

QR Code image

The QR Code image method has been deprecated. This DOES NOT include the keyuri method for generating the otpauth url.

Thus if you want to create the QR Code image, you will need to make use of a QR Code specific library to generate it. One example would be qrcode.

Sample code using qrcode library:

import qrcode from 'qrcode';
import otplib from 'otplib';

const otpauth = otplib.authenticator.keyuri('demo', 'otplib', secret);

qrcode.toDataURL(otpauth, function (err, imageUrl) {
  if (err) {
    console.log('Error with QR');
    return;
  }
  console.log(imageUrl);
});