Skip to content
Connie edited this page Jan 20, 2017 · 3 revisions

◄ Back (Two-Factor Usage)     Next (Verify The Token) ►

Displaying a QR code

Next, we'll want to display a QR code to the user so they can scan in the secret into their app. Google Authenticator and similar apps take in a QR code that holds a URL with the protocol otpauth://, which you get automatically from secret.otpauth_url.

Use a QR code module to generate a QR code that stores the data in secret.otpauth_url, and then display the QR code to the user. This is one simple way to do it, which generates a PNG data URL which you can put into an <img> tag on a webpage:

// Use the node-qrcode package
// npm install --save node-qrcode
var QRCode = require('qrcode');

// Get the data URL of the authenticator URL
QRCode.toDataURL(secret.otpauth_url, function(err, data_url) {
  console.log(data_url);

  // Display this data URL to the user in an <img> tag
  // Example:
  write('<img src="' + data_url + '">');
});

Ask the user to scan this QR code into their authenticator app.