auth-verify 1.13.5 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/otp/index.js +116 -13
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.5",
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/src/otp/index.js CHANGED
@@ -163,14 +163,34 @@ class OTPManager {
163
163
  if(this.senderConfig.apiService === 'sendgrid'){
164
164
  sgMail.setApiKey(this.senderConfig.apiKey)
165
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
+
166
174
  const apiMail = {
167
175
  to,
168
176
  from: this.senderConfig.sender,
169
177
  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
178
  };
173
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
+
174
194
  try{
175
195
  const info = await sgMail.send(apiMail)
176
196
  this.recieverConfig = options
@@ -184,14 +204,34 @@ class OTPManager {
184
204
  key: this.senderConfig.apiKey,
185
205
  });
186
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
+
187
215
  const apiMail = {
188
216
  to,
189
217
  from: this.senderConfig.sender,
190
218
  subject: subject || 'Your OTP Code',
191
- text: text || `Your OTP is ${this.code}`,
192
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
193
219
  };
194
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
+
195
235
  try{
196
236
  const info = await mg.messages.create(this.senderConfig.domain, apiMail)
197
237
  this.recieverConfig = options
@@ -202,14 +242,35 @@ class OTPManager {
202
242
  }else if(this.senderConfig.apiService === 'resend'){
203
243
  const resend = new Resend(this.senderConfig.apiKey);
204
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
+
205
253
  const apiMail = {
206
254
  to,
207
255
  from: this.senderConfig.sender,
208
256
  subject: subject || 'Your OTP Code',
209
- text: text || `Your OTP is ${this.code}`,
210
- html: html || `<p>Your OTP is <b>${this.code}</b></p>`,
211
257
  };
212
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
+
213
274
  try {
214
275
  const info = await resend.emails.send(apiMail);
215
276
  this.recieverConfig = options;
@@ -752,14 +813,56 @@ class OTPManager {
752
813
  // }
753
814
 
754
815
  async #sendEmailApi(reciever, mailOption) {
755
- await this.generate(mailOption.otpLen).set(reciever);
756
-
757
- 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,
758
840
  to: reciever,
759
- subject: mailOption.subject || "Your OTP code",
760
- text: mailOption.text || `Your OTP code is ${this.code}`,
761
- html: mailOption.html || `<p>Your OTP code is <b>${this.code}</b></p>`
762
- });
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;
763
866
  }
764
867
  }
765
868