auth-verify 1.13.4 → 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.4",
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 = {}){
@@ -253,14 +254,33 @@ class OTPManager {
253
254
  throw new Error(`Unsupported email service: ${this.senderConfig.service}`);
254
255
  }
255
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
+
256
265
  const mail = {
257
- from: this.senderConfig.sender,
258
266
  to,
267
+ from: this.senderConfig.sender,
259
268
  subject: subject || 'Your OTP Code',
260
- text: text || `Your OTP is ${this.code}`,
261
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
262
269
  };
263
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
+
264
284
  if (typeof callback === 'function') {
265
285
  transporter.sendMail(mail, (err, info) => {
266
286
  if (err) return callback(err);
@@ -622,7 +642,47 @@ class OTPManager {
622
642
  // console.log(otpCode);
623
643
  if(this.senderConfig.via === 'email'){
624
644
  if(this.senderConfig.service === 'smtp' || this.senderConfig.service === 'gmail'){
625
- 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);
626
686
  return true;
627
687
  }else{
628
688
  return await this.#sendEmailApi(reciever, mailOption);
@@ -644,17 +704,16 @@ class OTPManager {
644
704
  else return sendProcess();
645
705
  }
646
706
 
647
- #sendEmail(reciever, mailOption){
648
- return this.generate(mailOption.otpLen).set(reciever, (err)=>{
649
- if(err) throw err;
707
+ async #sendEmail(reciever, mailOption) {
708
+ await this.generate(mailOption.otpLen).set(reciever);
650
709
 
651
- this.message({
652
- to: reciever,
653
- subject: mailOption.subject || "Your OTP code",
654
- text: mailOption.text || `Your OTP code is ${this.code}`,
655
- html: mailOption.html || `Your OTP code is ${this.code}`
656
- });
657
- });
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
+ });
658
717
  }
659
718
 
660
719
  #sendSMS(reciever, smsOption){