auth-verify 1.13.4 → 1.13.6

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.6",
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 = {}){
@@ -162,14 +163,34 @@ class OTPManager {
162
163
  if(this.senderConfig.apiService === 'sendgrid'){
163
164
  sgMail.setApiKey(this.senderConfig.apiKey)
164
165
 
166
+ // const apiMail = {
167
+ // to,
168
+ // from: this.senderConfig.sender,
169
+ // subject: subject || 'Your OTP Code',
170
+ // text: text || `Your OTP is ${this.code}`,
171
+ // html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
172
+ // };
173
+
165
174
  const apiMail = {
166
175
  to,
167
176
  from: this.senderConfig.sender,
168
177
  subject: subject || 'Your OTP Code',
169
- text: text || `Your OTP is ${this.code}`,
170
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
171
178
  };
172
179
 
180
+ if (text !== undefined) {
181
+ apiMail.text = text;
182
+ }
183
+
184
+ if (html !== undefined) {
185
+ apiMail.html = html;
186
+ }
187
+
188
+ // fallback only if NOTHING provided
189
+ if (text === undefined && html === undefined) {
190
+ apiMail.text = `Your OTP is ${this.code}`;
191
+ apiMail.html = `<p>Your OTP is <b>${this.code}</b></p>`;
192
+ }
193
+
173
194
  try{
174
195
  const info = await sgMail.send(apiMail)
175
196
  this.recieverConfig = options
@@ -183,14 +204,34 @@ class OTPManager {
183
204
  key: this.senderConfig.apiKey,
184
205
  });
185
206
 
207
+ // const apiMail = {
208
+ // to,
209
+ // from: this.senderConfig.sender,
210
+ // subject: subject || 'Your OTP Code',
211
+ // text: text || `Your OTP is ${this.code}`,
212
+ // html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
213
+ // };
214
+
186
215
  const apiMail = {
187
216
  to,
188
217
  from: this.senderConfig.sender,
189
218
  subject: subject || 'Your OTP Code',
190
- text: text || `Your OTP is ${this.code}`,
191
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
192
219
  };
193
220
 
221
+ if (text !== undefined) {
222
+ apiMail.text = text;
223
+ }
224
+
225
+ if (html !== undefined) {
226
+ apiMail.html = html;
227
+ }
228
+
229
+ // fallback only if NOTHING provided
230
+ if (text === undefined && html === undefined) {
231
+ apiMail.text = `Your OTP is ${this.code}`;
232
+ apiMail.html = `<p>Your OTP is <b>${this.code}</b></p>`;
233
+ }
234
+
194
235
  try{
195
236
  const info = await mg.messages.create(this.senderConfig.domain, apiMail)
196
237
  this.recieverConfig = options
@@ -201,14 +242,35 @@ class OTPManager {
201
242
  }else if(this.senderConfig.apiService === 'resend'){
202
243
  const resend = new Resend(this.senderConfig.apiKey);
203
244
 
245
+ // const apiMail = {
246
+ // to,
247
+ // from: this.senderConfig.sender,
248
+ // subject: subject || 'Your OTP Code',
249
+ // text: text || `Your OTP is ${this.code}`,
250
+ // html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
251
+ // };
252
+
204
253
  const apiMail = {
205
254
  to,
206
255
  from: this.senderConfig.sender,
207
256
  subject: subject || 'Your OTP Code',
208
- text: text || `Your OTP is ${this.code}`,
209
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
210
257
  };
211
258
 
259
+ if (text !== undefined) {
260
+ apiMail.text = text;
261
+ }
262
+
263
+ if (html !== undefined) {
264
+ apiMail.html = html;
265
+ }
266
+
267
+ // fallback only if NOTHING provided
268
+ if (text === undefined && html === undefined) {
269
+ apiMail.text = `Your OTP is ${this.code}`;
270
+ apiMail.html = `<p>Your OTP is <b>${this.code}</b></p>`;
271
+ }
272
+
273
+
212
274
  try {
213
275
  const info = await resend.emails.send(apiMail);
214
276
  this.recieverConfig = options;
@@ -253,14 +315,33 @@ class OTPManager {
253
315
  throw new Error(`Unsupported email service: ${this.senderConfig.service}`);
254
316
  }
255
317
 
318
+ // const mail = {
319
+ // from: this.senderConfig.sender,
320
+ // to,
321
+ // subject: subject || 'Your OTP Code',
322
+ // text: text || `Your OTP is ${this.code}`,
323
+ // html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
324
+ // };
325
+
256
326
  const mail = {
257
- from: this.senderConfig.sender,
258
327
  to,
328
+ from: this.senderConfig.sender,
259
329
  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
330
  };
263
331
 
332
+ if (text !== undefined) {
333
+ mail.text = text;
334
+ }
335
+
336
+ if (html !== undefined) {
337
+ mail.html = html;
338
+ }
339
+
340
+ // fallback only if NOTHING provided
341
+ if (text === undefined && html === undefined) {
342
+ mail.text = `Your OTP is ${this.code}`;
343
+ }
344
+
264
345
  if (typeof callback === 'function') {
265
346
  transporter.sendMail(mail, (err, info) => {
266
347
  if (err) return callback(err);
@@ -622,7 +703,47 @@ class OTPManager {
622
703
  // console.log(otpCode);
623
704
  if(this.senderConfig.via === 'email'){
624
705
  if(this.senderConfig.service === 'smtp' || this.senderConfig.service === 'gmail'){
625
- await this.#sendEmail(reciever, mailOption);
706
+ // await this.#sendEmail(reciever, mailOption);
707
+ this.generate(mailOption.otpLen);
708
+ await this.set(reciever);
709
+
710
+ const resolvedHtml =
711
+ typeof mailOption.html === 'function'
712
+ ? mailOption.html(this.code)
713
+ : mailOption.html;
714
+
715
+ const resolvedText =
716
+ typeof mailOption.text === 'function'
717
+ ? mailOption.text(this.code)
718
+ : mailOption.text;
719
+
720
+ const mail = {
721
+ from: this.senderConfig.sender,
722
+ to: reciever,
723
+ subject: mailOption.subject || 'Your OTP Code',
724
+ };
725
+
726
+ if (mailOption.text) {
727
+ mail.text = resolvedText;
728
+ }
729
+
730
+ if (mailOption.html) {
731
+ mail.html = resolvedHtml;
732
+ }
733
+
734
+ // fallback if neither provided
735
+ if (!mailOption.text && !mailOption.html) {
736
+ mail.text = `Your OTP is ${this.code}`;
737
+ }
738
+
739
+ // this.message({
740
+ // to: reciever,
741
+ // subject: mailOption.subject || "Email verification",
742
+ // html: resolvedHtml || `Your OTP code is <b>${this.code}</b>`,
743
+ // text: resolvedText || `Your OTP code is ${this.code}`,
744
+ // });
745
+
746
+ this.message(mail);
626
747
  return true;
627
748
  }else{
628
749
  return await this.#sendEmailApi(reciever, mailOption);
@@ -644,17 +765,16 @@ class OTPManager {
644
765
  else return sendProcess();
645
766
  }
646
767
 
647
- #sendEmail(reciever, mailOption){
648
- return this.generate(mailOption.otpLen).set(reciever, (err)=>{
649
- if(err) throw err;
768
+ async #sendEmail(reciever, mailOption) {
769
+ await this.generate(mailOption.otpLen).set(reciever);
650
770
 
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
- });
771
+
772
+ return await this.message({
773
+ to: reciever,
774
+ subject: mailOption.subject || "Your OTP code",
775
+ text: mailOption.text || `Your OTP code is ${this.code}`,
776
+ html: mailOption.html || `<p>Your OTP code is <b>${this.code}</b></p>`
777
+ });
658
778
  }
659
779
 
660
780
  #sendSMS(reciever, smsOption){
@@ -693,14 +813,56 @@ class OTPManager {
693
813
  // }
694
814
 
695
815
  async #sendEmailApi(reciever, mailOption) {
696
- await this.generate(mailOption.otpLen).set(reciever);
697
-
698
- return await this.message({
816
+ // await this.generate(mailOption.otpLen).set(reciever);
817
+
818
+ // return await this.message({
819
+ // to: reciever,
820
+ // subject: mailOption.subject || "Your OTP code",
821
+ // text: mailOption.text || `Your OTP code is ${this.code}`,
822
+ // html: mailOption.html || `<p>Your OTP code is <b>${this.code}</b></p>`
823
+ // });
824
+
825
+ this.generate(mailOption.otpLen);
826
+ await this.set(reciever);
827
+
828
+ const resolvedHtml =
829
+ typeof mailOption.html === 'function'
830
+ ? mailOption.html(this.code)
831
+ : mailOption.html;
832
+
833
+ const resolvedText =
834
+ typeof mailOption.text === 'function'
835
+ ? mailOption.text(this.code)
836
+ : mailOption.text;
837
+
838
+ const mail = {
839
+ from: this.senderConfig.sender,
699
840
  to: reciever,
700
- subject: mailOption.subject || "Your OTP code",
701
- text: mailOption.text || `Your OTP code is ${this.code}`,
702
- html: mailOption.html || `<p>Your OTP code is <b>${this.code}</b></p>`
703
- });
841
+ subject: mailOption.subject || 'Your OTP Code',
842
+ };
843
+
844
+ if (mailOption.text) {
845
+ mail.text = resolvedText;
846
+ }
847
+
848
+ if (mailOption.html) {
849
+ mail.html = resolvedHtml;
850
+ }
851
+
852
+ // fallback if neither provided
853
+ if (!mailOption.text && !mailOption.html) {
854
+ mail.text = `Your OTP is ${this.code}`;
855
+ }
856
+
857
+ // this.message({
858
+ // to: reciever,
859
+ // subject: mailOption.subject || "Email verification",
860
+ // html: resolvedHtml || `Your OTP code is <b>${this.code}</b>`,
861
+ // text: resolvedText || `Your OTP code is ${this.code}`,
862
+ // });
863
+
864
+ this.message(mail);
865
+ return true;
704
866
  }
705
867
  }
706
868