Skip to content

Commit

Permalink
Merge pull request #163 from madarche/doc-exception
Browse files Browse the repository at this point in the history
Document that check and verify methods can throw
  • Loading branch information
yeojz authored Apr 22, 2019
2 parents 8a93a6a + d7c14b5 commit f81a4f1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,20 @@ in the [releases](https://github.com/yeojz/otplib/releases) page.
```js
import otplib from 'otplib';

const secret = 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD'
const secret = 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD';
// Alternatively: const secret = otplib.authenticator.generateSecret();

const token = otplib.authenticator.generate(secret);

const isValid = otplib.authenticator.check(token, secret);
// or
const isValid = otplib.authenticator.verify({ token, secret })
try {
const isValid = otplib.authenticator.check(token, secret);
// or
const isValid = otplib.authenticator.verify({ token, secret });
} catch (err) {
// Error possibly thrown by the thirty-two package
// 'Invalid input - it is not base32 encoded string'
console.error(err);
}
```

#### Using specific OTP implementations
Expand Down Expand Up @@ -341,13 +347,13 @@ import otplib from 'otplib';
const user = 'A user name, possibly an email';
const service = 'A service name';

// v11.x.x and above
const otpauth = otplib.authenticator.keyuri(user, service, secret);

// v10.x.x and below
const otpauth = otplib.authenticator.keyuri(
encodeURIComponent(user), encodeURIComponent(service), secret);

// v11.x.x and above
const otpauth = otplib.authenticator.keyuri(user, service, secret);

qrcode.toDataURL(otpauth, (err, imageUrl) => {
if (err) {
console.log('Error with QR');
Expand Down

0 comments on commit f81a4f1

Please sign in to comment.