-
-
Notifications
You must be signed in to change notification settings - Fork 134
Upgrading v3 to v4
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.
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.
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;
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.
The keyuri
method now returns non-encoded URIs. So if you need to encode it, you will have to run it through encodeURIComponent
manually.
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.
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);
});