auth-verify 1.13.1 → 1.13.2

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
@@ -1,22 +1,25 @@
1
1
  {
2
2
  "dependencies": {
3
+ "@sendgrid/mail": "^8.1.6",
3
4
  "axios": "^1.12.2",
4
5
  "base64url": "^3.0.1",
5
6
  "cbor": "^10.0.11",
6
7
  "cors": "^2.8.5",
7
8
  "crypto": "^1.0.1",
8
9
  "express": "^5.1.0",
10
+ "form-data": "^4.0.5",
9
11
  "ioredis": "^5.8.1",
10
12
  "jsonwebtoken": "^9.0.2",
11
- "mailage": "^0.0.1",
13
+ "mailgun.js": "^12.7.0",
12
14
  "node-telegram-bot-api": "^0.66.0",
13
15
  "nodemailer": "^7.0.6",
14
16
  "path": "^0.12.7",
15
17
  "qrcode": "^1.5.4",
18
+ "resend": "^6.9.1",
16
19
  "uuid": "^9.0.1"
17
20
  },
18
21
  "name": "auth-verify",
19
- "version": "1.13.1",
22
+ "version": "1.13.2",
20
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",
21
24
  "main": "index.js",
22
25
  "scripts": {
package/readme.md CHANGED
@@ -344,16 +344,20 @@ otp.sender({
344
344
  })
345
345
  ```
346
346
 
347
- <!-- #### Using API
347
+ #### Using API
348
348
  ```js
349
349
  otp.sender({
350
350
  via: "email",
351
351
  service: "api",
352
352
  sender: "your_email@gmail.com",
353
- apiService: "resend", // "postmark", "sendgrid"
353
+ apiService: "resend", // "mailgun", "sendgrid"
354
354
  apiKey: "your-api-key"
355
355
  })
356
- ``` -->
356
+ ```
357
+ > For `"mailgun"` you should also add also your domain
358
+ > ```js
359
+ > domain: "your-domain.com"
360
+ >```
357
361
 
358
362
  #### SMS sender
359
363
  #### 📘 Auth-Verify SMS API — Full Guide
package/src/otp/index.js CHANGED
@@ -2,7 +2,10 @@ const {generateSecureOTP, resendGeneratedOTP, sendSMS} = require('../helpers/hel
2
2
  const Redis = require("ioredis");
3
3
  const nodemailer = require("nodemailer");
4
4
  const TelegramBot = require("node-telegram-bot-api");
5
- // const { Transport, Resend, SendGrid, Postmark } = require('mailage')
5
+ const sgMail = require('@sendgrid/mail')
6
+ const formData = require('form-data');
7
+ const Mailgun = require('mailgun.js');
8
+ const { Resend } = require('resend');
6
9
 
7
10
  class OTPManager {
8
11
  constructor(otpOptions = {}){
@@ -152,6 +155,74 @@ class OTPManager {
152
155
 
153
156
  // ---- EMAIL PART ----
154
157
  if (this.senderConfig.via === 'email') {
158
+
159
+ // ---- API based ----
160
+
161
+ if(this.senderConfig.service === 'api'){
162
+ if(this.senderConfig.apiService === 'sendgrid'){
163
+ sgMail.setApiKey(this.senderConfig.apiKey)
164
+
165
+ const apiMail = {
166
+ to,
167
+ from: this.senderConfig.sender,
168
+ 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
+ };
172
+
173
+ try{
174
+ const info = await sgMail.send(apiMail)
175
+ this.recieverConfig = options
176
+ return info
177
+ }catch(err){
178
+ throw new Error(err.response?.body || err.message)
179
+ }
180
+ }else if(this.senderConfig.apiService === 'mailgun'){
181
+ const mg = new Mailgun(formData).client({
182
+ username: 'api',
183
+ key: this.senderConfig.apiKey,
184
+ });
185
+
186
+ const apiMail = {
187
+ to,
188
+ from: this.senderConfig.sender,
189
+ 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
+ };
193
+
194
+ try{
195
+ const info = await mg.messages.create(this.senderConfig.domain, apiMail)
196
+ this.recieverConfig = options
197
+ return info
198
+ }catch(err){
199
+ throw new Error(err.response?.body || err.message)
200
+ }
201
+ }else if(this.senderConfig.apiService === 'resend'){
202
+ const resend = new Resend(this.senderConfig.apiKey);
203
+
204
+ const apiMail = {
205
+ to,
206
+ from: this.senderConfig.sender,
207
+ 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
+ };
211
+
212
+ try {
213
+ const info = await resend.emails.send(apiMail);
214
+ this.recieverConfig = options;
215
+ return info;
216
+ } catch(err) {
217
+ throw new Error(err.response?.body || err.message);
218
+ }
219
+ }else {
220
+ throw new Error(`Unsupported API email service: ${this.senderConfig.apiService}`);
221
+ }
222
+ }
223
+
224
+ // ---- SMTP -----
225
+
155
226
  let transporter;
156
227
 
157
228
  if (this.senderConfig.service === 'gmail') {
@@ -171,45 +242,7 @@ class OTPManager {
171
242
  pool: true,
172
243
  maxConnections: 3,
173
244
  maxMessages: 50
174
- });
175
- // }else if(this.senderConfig.service === 'api'){
176
- // if(this.senderConfig.apiService === 'sendgrid'){
177
-
178
- // (async function(){
179
- // const apiMail = new Transport(
180
- // new SendGrid({
181
- // apiKey: this.senderConfig.apiKey,
182
- // sender: this.senderConfig.sender
183
- // })
184
- // )
185
-
186
- // apiMail.send({to,subject,text, html})
187
- // })()
188
- // }else if(this.senderConfig.apiService === 'resend'){
189
- // (async function() {
190
- // const apiMail = new Transport(
191
- // new Resend({
192
- // apiKey: this.senderConfig.apiKey,
193
- // sender: this.senderConfig.sender
194
- // })
195
- // )
196
-
197
- // apiMail.send({to, subject, text, html})
198
- // })()
199
- // }else if(this.senderConfig.apiService === 'postmark'){
200
- // (async function(params) {
201
- // const apiMail = new Transport(
202
- // new Postmark({
203
- // apiKey: this.senderConfig.apiKey,
204
- // sender: this.senderConfig.sender
205
- // })
206
- // )
207
-
208
- // apiMail.send({to, subject, text, html})
209
- // })()
210
- // }else{
211
- // throw new Error(`Unsupported email service: ${this.senderConfig.apiService}`);
212
- // }
245
+ });
213
246
  } else {
214
247
  throw new Error(`Unsupported email service: ${this.senderConfig.service}`);
215
248
  }
@@ -582,17 +615,20 @@ class OTPManager {
582
615
  // const otpCode = this.generate(mailOption.otpLen = 6).set(reciever);
583
616
  // console.log(otpCode);
584
617
  if(this.senderConfig.via === 'email'){
585
- await this.#sendEmail(reciever, mailOption);
586
- return true;
618
+ if(this.senderConfig.service === 'smtp' || this.senderConfig.service === 'gmail'){
619
+ await this.#sendEmail(reciever, mailOption);
620
+ return true;
621
+ }else{
622
+ return await this.#sendEmailApi(reciever, mailOption);
623
+ // console.log('API')
624
+ // return true;
625
+ }
587
626
  }else if(this.senderConfig.via === 'sms'){
588
627
  await this.#sendSMS(reciever, mailOption);
589
628
  return true;
590
629
  }else if(this.senderConfig.via === 'whatsapp'){
591
630
  await this.#sendWhatsApp(reciever, mailOption);
592
631
  return true;
593
- // }else if(this.senderConfig.via === 'api'){
594
- // await this.#sendEmailApi(reciever, mailOption);
595
- // return true;
596
632
  }else{
597
633
  throw new Error("senderConfig.via should be 'email' or 'sms'")
598
634
  }
@@ -649,6 +685,17 @@ class OTPManager {
649
685
  // })
650
686
  // })
651
687
  // }
688
+
689
+ async #sendEmailApi(reciever, mailOption) {
690
+ await this.generate(mailOption.otpLen).set(reciever);
691
+
692
+ return await this.message({
693
+ to: reciever,
694
+ subject: mailOption.subject || "Your OTP code",
695
+ text: mailOption.text || `Your OTP code is ${this.code}`,
696
+ html: mailOption.html || `<p>Your OTP code is <b>${this.code}</b></p>`
697
+ });
698
+ }
652
699
  }
653
700
 
654
701
  module.exports = OTPManager;