ExpressJS + 2FA

npm i 2fa

 

generate QR code

app.get('/code', (req, res, next) => {
  tfa.generateKey(32, (err, key) => {

    process.env.KEY = key;
    console.log(err);
    console.log(key);
    tfa.generateBackupCodes(4, 'xxxx-xxxx-xxxx', (err, codes) => {
      console.log(err);
      console.log(codes);
    });

    tfa.generateGoogleQR('TwoFA', 'email@email.com', key, (err, qr) => {
      console.log(err);
      console.log(qr);

      res.send(`<img src="${qr}" />`);
    });
  });

});

 

verify TOTP

app.get('/verify/:code', (req, res, next) => {
  
  const code = req.params.code;
  console.log(code);

  const options = {
    beforeDrift: 2,
    afterDrift: 2,
    drift: 4,
    step: 30
  };

  const result = tfa.verifyTOTP(process.env.KEY, code, options);

  res.send(result);
});
创建时间:10/26/2022 1:32:15 PM 修改时间:10/26/2022 1:33:38 PM