expresso
  • Get Started
    • 👋Welcome!
    • ⚡Quick Start
  • Expresso Library
    • 📖Introduction
    • ⚛️expresso-core
      • Currency
      • Random String
      • Formatter
      • File
      • Phone
    • ⚓expresso-hooks
      • Multer
      • Token
      • OTP
    • 🚀expresso-provider
      • FCM
      • SMTP
      • Redis
      • Storage
    • 🔗expresso-query
      • TypeORM
      • Sequelize
Powered by GitBook
On this page
  • Hash OTP
  • Verify OTP
  1. Expresso Library
  2. expresso-hooks

OTP

handle one time password made easier

Using commonjs

const { useOTP } = require('expresso-hooks')

Using ES6

import { useOTP } from 'expresso-hooks'

Hash OTP

const otp = new useOTP()

const anyPhone = '081234567890'
const anyOTP = '123456'
const anySecretKey = '1234567890abcdef'

const payload = JSON.stringify({ phone: anyPhone, otp: anyOTP })

const data = otp.hashOTP(payload, {
  expires: '1d',
  secretKey: anySecretKey,
})

console.log(data)

// output
439f8f1555a9266fbad880c45b6262216a94c8ac8e2def2468cd423323e7ad3c.1691548132497

Verify OTP

const otp = new useOTP()

const anyPhone = '081234567890'
const anyOTP = '123456'
const anySecretKey = '1234567890abcdef'

const payload = JSON.stringify({ phone: anyPhone, otp: anyOTP })

const hashOTP = otp.hashOTP(payload, {
  expires: '1d',
  secretKey: anySecretKey,
})

const verifyHash = otp.verifyHash(payload, hashOTP, {
  secretKey: anySecretKey,
})

console.log(verifyHash)

// output
true
PreviousTokenNextexpresso-provider

Last updated 1 year ago

⚓