Skip to content

Instantly share code, notes, and snippets.

@PatrickKalkman
Created January 9, 2022 19:08
Show Gist options
  • Save PatrickKalkman/36d526cebc05befe86bc840e390a7722 to your computer and use it in GitHub Desktop.
Save PatrickKalkman/36d526cebc05befe86bc840e390a7722 to your computer and use it in GitHub Desktop.
Enabling two factor authentication step 2
userController.enableTwoFactorAuthStep2 = function (req, reply) {
tokenVerification.extractAndVerifyToken(req, (err, isValidJwtToken, email) => {
if (!err && isValidJwtToken) {
const user = db.getUser(email);
if (typeof user !== 'undefined') {
log.info(req.body);
const base32secret = req.body.base32;
const userToken = req.body.token;
const verified = speakeasy.totp.verify({ secret: base32secret, encoding: 'base32', token: userToken });
if (verified) {
db.enableTwoFactorAuthentication(email);
reply.code(200).send({ validated: true });
} else {
reply.code(200).send({ validated: false });
}
}
} else {
reply.unauthorized(err);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment