auth-verify 1.13.3 → 1.13.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "uuid": "^9.0.1"
20
20
  },
21
21
  "name": "auth-verify",
22
- "version": "1.13.3",
22
+ "version": "1.13.5",
23
23
  "description": "A simple Node.js library for sending and verifying OTP via email, SMS and Telegram bot. And generating TOTP codes and QR codes. And handling JWT with Cookies. And also handling passwordless logins with passkeys/webauth. And handling magiclink passwordless logins",
24
24
  "main": "index.js",
25
25
  "scripts": {
package/readme.md CHANGED
@@ -495,16 +495,18 @@ await otp.set("user@example.com");
495
495
  - Can also use **callback style** for memory storage
496
496
 
497
497
  ### 📤 Send OTP
498
+ First way
498
499
  ```js
499
500
  await otp.send("user@example.com", {
500
501
  subject: "Your OTP Code",
501
- text: "Your OTP is 123456",
502
- html: "<b>123456</b>"
502
+ text: code => `Your OTP is ${code}`,
503
+ html: code => `<b>${code}</b>`
503
504
  });
504
505
 
505
506
  // or simply
506
507
  await otp.send("user@example.com");
507
508
  ```
509
+
508
510
  Supports channels:
509
511
  | `via` | Notes |
510
512
  | -------- | ---------------------------------------- |
package/src/otp/index.js CHANGED
@@ -6,6 +6,7 @@ const sgMail = require('@sendgrid/mail')
6
6
  const formData = require('form-data');
7
7
  const Mailgun = require('mailgun.js');
8
8
  const { Resend } = require('resend');
9
+ const mail = require('@sendgrid/mail');
9
10
 
10
11
  class OTPManager {
11
12
  constructor(otpOptions = {}){
@@ -227,8 +228,14 @@ class OTPManager {
227
228
 
228
229
  if (this.senderConfig.service === 'gmail') {
229
230
  transporter = nodemailer.createTransport({
230
- service: 'gmail',
231
- auth: { user: this.senderConfig.sender, pass: this.senderConfig.pass },
231
+ host: "smtp.gmail.com",
232
+ port: this.senderConfig.port || 587,
233
+ secure: false,
234
+ requireTLS: true,
235
+ auth: {
236
+ user: this.senderConfig.sender,
237
+ pass: this.senderConfig.pass, // app password
238
+ },
232
239
  pool: true,
233
240
  maxConnections: 3,
234
241
  maxMessages: 50
@@ -247,14 +254,33 @@ class OTPManager {
247
254
  throw new Error(`Unsupported email service: ${this.senderConfig.service}`);
248
255
  }
249
256
 
257
+ // const mail = {
258
+ // from: this.senderConfig.sender,
259
+ // to,
260
+ // subject: subject || 'Your OTP Code',
261
+ // text: text || `Your OTP is ${this.code}`,
262
+ // html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
263
+ // };
264
+
250
265
  const mail = {
251
- from: this.senderConfig.sender,
252
266
  to,
267
+ from: this.senderConfig.sender,
253
268
  subject: subject || 'Your OTP Code',
254
- text: text || `Your OTP is ${this.code}`,
255
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
256
269
  };
257
270
 
271
+ if (text !== undefined) {
272
+ mail.text = text;
273
+ }
274
+
275
+ if (html !== undefined) {
276
+ mail.html = html;
277
+ }
278
+
279
+ // fallback only if NOTHING provided
280
+ if (text === undefined && html === undefined) {
281
+ mail.text = `Your OTP is ${this.code}`;
282
+ }
283
+
258
284
  if (typeof callback === 'function') {
259
285
  transporter.sendMail(mail, (err, info) => {
260
286
  if (err) return callback(err);
@@ -616,7 +642,47 @@ class OTPManager {
616
642
  // console.log(otpCode);
617
643
  if(this.senderConfig.via === 'email'){
618
644
  if(this.senderConfig.service === 'smtp' || this.senderConfig.service === 'gmail'){
619
- await this.#sendEmail(reciever, mailOption);
645
+ // await this.#sendEmail(reciever, mailOption);
646
+ this.generate(mailOption.otpLen);
647
+ await this.set(reciever);
648
+
649
+ const resolvedHtml =
650
+ typeof mailOption.html === 'function'
651
+ ? mailOption.html(this.code)
652
+ : mailOption.html;
653
+
654
+ const resolvedText =
655
+ typeof mailOption.text === 'function'
656
+ ? mailOption.text(this.code)
657
+ : mailOption.text;
658
+
659
+ const mail = {
660
+ from: this.senderConfig.sender,
661
+ to: reciever,
662
+ subject: mailOption.subject || 'Your OTP Code',
663
+ };
664
+
665
+ if (mailOption.text) {
666
+ mail.text = resolvedText;
667
+ }
668
+
669
+ if (mailOption.html) {
670
+ mail.html = resolvedHtml;
671
+ }
672
+
673
+ // fallback if neither provided
674
+ if (!mailOption.text && !mailOption.html) {
675
+ mail.text = `Your OTP is ${this.code}`;
676
+ }
677
+
678
+ // this.message({
679
+ // to: reciever,
680
+ // subject: mailOption.subject || "Email verification",
681
+ // html: resolvedHtml || `Your OTP code is <b>${this.code}</b>`,
682
+ // text: resolvedText || `Your OTP code is ${this.code}`,
683
+ // });
684
+
685
+ this.message(mail);
620
686
  return true;
621
687
  }else{
622
688
  return await this.#sendEmailApi(reciever, mailOption);
@@ -638,17 +704,16 @@ class OTPManager {
638
704
  else return sendProcess();
639
705
  }
640
706
 
641
- #sendEmail(reciever, mailOption){
642
- return this.generate(mailOption.otpLen).set(reciever, (err)=>{
643
- if(err) throw err;
707
+ async #sendEmail(reciever, mailOption) {
708
+ await this.generate(mailOption.otpLen).set(reciever);
644
709
 
645
- this.message({
646
- to: reciever,
647
- subject: mailOption.subject || "Your OTP code",
648
- text: mailOption.text || `Your OTP code is ${this.code}`,
649
- html: mailOption.html || `Your OTP code is ${this.code}`
650
- });
651
- });
710
+
711
+ return await this.message({
712
+ to: reciever,
713
+ subject: mailOption.subject || "Your OTP code",
714
+ text: mailOption.text || `Your OTP code is ${this.code}`,
715
+ html: mailOption.html || `<p>Your OTP code is <b>${this.code}</b></p>`
716
+ });
652
717
  }
653
718
 
654
719
  #sendSMS(reciever, smsOption){