Created
November 11, 2023 03:43
-
-
Save 1mehdifaraji/cd2e1009db6643bb1feaeb9688f13979 to your computer and use it in GitHub Desktop.
OTP generation and verification with speakeasy library in express js framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import speakeasy from "speakeasy"; | |
export const generateOtp = (secret: string): string => | |
speakeasy.time({ | |
encoding: "base32", | |
secret, | |
digits: 4, | |
step: 300, // expire after 5 mins | |
}); | |
export const verifyOtp = (secret: string, token: string): boolean => | |
speakeasy.time.verify({ | |
encoding: "base32", | |
token, | |
secret, | |
digits: 4, | |
step: 300, // expire after 5 mins | |
}); | |
// How to use | |
// const secret = generateOtp(String(req.query.phone)); | |
// const verified = verifyOtp(String(req.query.phone), String(req.query.otp)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment