Skip to content

Instantly share code, notes, and snippets.

@1mehdifaraji
Created November 11, 2023 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1mehdifaraji/cd2e1009db6643bb1feaeb9688f13979 to your computer and use it in GitHub Desktop.
Save 1mehdifaraji/cd2e1009db6643bb1feaeb9688f13979 to your computer and use it in GitHub Desktop.
OTP generation and verification with speakeasy library in express js framework
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