1

I am using https://www.npmjs.com/package/speakeasy to generate OTP and i would like the expiry to 10 minutes.

Here is the code for generation

const generateOtp = function generateOtp() {
    let token = speakeasy.totp({
        secret:process.env.OTP_KEY,
        encoding: 'base32',
        digits:4,
        window:10
    });
    return token;
}

Verify OTP

const verifyOtp = function verifyOtp(token){
    let expiry =  speakeasy.totp.verify({
        secret:process.env.OTP_KEY,
        encoding: 'base32',
        token: token,
        window:10
    });
    console.log(expiry)
}

But I don't know how to set the expiry to 10 minutes??

shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129

1 Answers1

1

Reading the documentation you can find out that the base step is 30 seconds, so if you want to have an expiration time of 10 minutes you need to set up the step to 60. Then, using the verifyDelta method you should be able to check if the token expired.

const generateOtp = function generateOtp() {
    let token = speakeasy.totp({
        secret:process.env.OTP_KEY,
        encoding: 'base32',
        digits:4,
        step: 60,
        window:10
    });
    return token;
}

const verifyOtp = function verifyOtp(token){
    let expiry =  speakeasy.totp.verifyDelta({
        secret:process.env.OTP_KEY,
        encoding: 'base32',
        token: token,
        step: 60,
        window:10
    });
    console.log(expiry)
}
Dez
  • 5,702
  • 8
  • 42
  • 51