otplib icon indicating copy to clipboard operation
otplib copied to clipboard

2fa is not working on live server in nodejs

Open farhanali2325 opened this issue 2 years ago • 2 comments

Describe the bug I've implemented optlib in node js and it's working fine on the localhost but not on the live server.

Following is the backed code where I am checking it.

exports.enableTwoFactorAuthentication = async (req, res) => {
    try {
        let data = req.body;
        console.log("data: ", data)
        const { userId, one_time_password } = data;
        const user = await CRMUser.findById(userId);

        const { tfa_auth_secret_key } = user;
        console.log('user', tfa_auth_secret_key, user)
        console.log("authenticator.check(one_time_password, tfa_auth_secret_key): ",authenticator.check(one_time_password, tfa_auth_secret_key))
        if (!one_time_password || !authenticator.check(one_time_password, tfa_auth_secret_key)) {
            return res.status(200).send({ success: false, message: 'Invalid 2FA Code.'});
        }
        else {
            user.tfa_auth_status = true;
            await user.save();

            return res.status(200).json({ success: true, message: '2FA enabled successfully.', tfa_auth_status: true, secretKey: user.tfa_auth_secret_key, qrImage: user.tfa_auth_qr_image });
        }
    } catch (error) {
        return res.status(500).json({ success: false, message: error.message })
    }
};

Can somebody help me?

farhanali2325 avatar Feb 15 '23 16:02 farhanali2325

You have to use .verify() method instead of .check() in order to verify the code.

lpavliuk avatar Apr 26 '23 02:04 lpavliuk