如何在 Node.js 中使用基于 API 的提供商发送 OTP
发布: (2026年2月3日 GMT+8 16:16)
1 min read
原文: Dev.to
Source: Dev.to
设置
从 npm 安装所需的库:
npm install auth-verify
配置
const AuthVerify = require('auth-verify');
const auth = new AuthVerify();
// Configure the OTP sender
auth.otp.sender({
via: 'email',
service: 'api',
apiService: 'resend', // Change to 'mailgun', 'sendgrid', etc. as needed
apiKey: 'YOUR_API_KEY_HERE'
});
发送 OTP
await auth.otp.send('johndoe@example.com', {
otpLen: 5, // Length of OTP codes (default: 6)
subject: 'Account verification',
text: `Your OTP code is ${auth.otp.code}`
});
验证 OTP
const valid = await auth.otp.verify('johndoe@example.com', '12345');
if (valid) {
console.log('User verified!');
} else {
console.log('Invalid code!');
}