irismail 0.1.0
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/README.md +158 -0
- package/dist/chunk-FTNSOYOW.mjs +223 -0
- package/dist/chunk-QZ7TP4HQ.mjs +7 -0
- package/dist/chunk-XGASTZZ6.mjs +180 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +446 -0
- package/dist/index.mjs +31 -0
- package/dist/react/index.d.mts +60 -0
- package/dist/react/index.d.ts +60 -0
- package/dist/react/index.js +261 -0
- package/dist/react/index.mjs +15 -0
- package/dist/server/index.d.mts +110 -0
- package/dist/server/index.d.ts +110 -0
- package/dist/server/index.js +218 -0
- package/dist/server/index.mjs +15 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# IrisMail
|
|
2
|
+
|
|
3
|
+
A modular, secure, and easy-to-use Email and OTP service for Next.js applications, with built-in Shadcn UI support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 📧 **Email Service**: Simple wrapper around Nodemailer for sending emails.
|
|
8
|
+
- 🔐 **Secure OTP**: Server-side OTP generation, encryption, and validation.
|
|
9
|
+
- 🎨 **Shadcn UI**: Ready-to-use OTP Input component compatible with Shadcn UI.
|
|
10
|
+
- ⚡ **React Hook**: `useOTP` hook for managing timers, rate limiting, and resend cooldowns.
|
|
11
|
+
- 🛡️ **Type-Safe**: Built with TypeScript.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install irismail
|
|
17
|
+
# or
|
|
18
|
+
yarn add irismail
|
|
19
|
+
# or
|
|
20
|
+
pnpm add irismail
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### 1. Server-Side Setup (API Routes)
|
|
26
|
+
|
|
27
|
+
Initialize the service with your email configuration and a secret key for encryption.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// app/api/send-otp/route.ts
|
|
31
|
+
import { IrisMailService } from 'irismail/server';
|
|
32
|
+
import { NextResponse } from 'next/server';
|
|
33
|
+
|
|
34
|
+
const irismail = new IrisMailService({
|
|
35
|
+
email: {
|
|
36
|
+
transport: {
|
|
37
|
+
host: 'smtp.gmail.com',
|
|
38
|
+
port: 587,
|
|
39
|
+
auth: {
|
|
40
|
+
user: process.env.GMAIL_USER,
|
|
41
|
+
pass: process.env.GMAIL_PASS,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
defaults: {
|
|
45
|
+
from: {
|
|
46
|
+
name: 'My App',
|
|
47
|
+
address: process.env.GMAIL_USER,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
secretKey: process.env.OTP_SECRET_KEY, // Must be 32 chars
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export async function POST(req: Request) {
|
|
55
|
+
const { email } = await req.json();
|
|
56
|
+
|
|
57
|
+
// Generate and encrypt OTP
|
|
58
|
+
const otp = irismail.otp.generateOTP();
|
|
59
|
+
const encryptedOtp = irismail.otp.encryptOTP(otp);
|
|
60
|
+
|
|
61
|
+
// Send email
|
|
62
|
+
await irismail.email.sendEmail({
|
|
63
|
+
to: email,
|
|
64
|
+
subject: 'Your Verification Code',
|
|
65
|
+
html: `<p>Your code is: <b>${otp}</b></p>`,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return NextResponse.json({ encryptedOtp });
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 2. Client-Side Setup (React Component)
|
|
73
|
+
|
|
74
|
+
Use the `InputOTP` component and `useOTP` hook in your verification form.
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
// components/verify-form.tsx
|
|
78
|
+
'use client';
|
|
79
|
+
|
|
80
|
+
import { useState } from 'react';
|
|
81
|
+
import { InputOTP, InputOTPGroup, InputOTPSlot, useOTP } from 'irismail/react';
|
|
82
|
+
|
|
83
|
+
export function VerifyForm({ email }) {
|
|
84
|
+
const [otp, setOtp] = useState('');
|
|
85
|
+
const { otpTimeLeft, resendTimeLeft, formatTime } = useOTP({ email });
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<div className="space-y-4">
|
|
89
|
+
<InputOTP
|
|
90
|
+
maxLength={6}
|
|
91
|
+
value={otp}
|
|
92
|
+
onChange={setOtp}
|
|
93
|
+
>
|
|
94
|
+
<InputOTPGroup>
|
|
95
|
+
<InputOTPSlot index={0} />
|
|
96
|
+
<InputOTPSlot index={1} />
|
|
97
|
+
<InputOTPSlot index={2} />
|
|
98
|
+
<InputOTPSlot index={3} />
|
|
99
|
+
<InputOTPSlot index={4} />
|
|
100
|
+
<InputOTPSlot index={5} />
|
|
101
|
+
</InputOTPGroup>
|
|
102
|
+
</InputOTP>
|
|
103
|
+
|
|
104
|
+
<div className="text-sm">
|
|
105
|
+
Time remaining: {formatTime(otpTimeLeft)}
|
|
106
|
+
</div>
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Local Testing
|
|
113
|
+
|
|
114
|
+
Want to test the package locally before publishing? We've included a complete example app!
|
|
115
|
+
|
|
116
|
+
1. **Navigate to the example directory**:
|
|
117
|
+
```bash
|
|
118
|
+
cd example
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
2. **Set up environment variables**:
|
|
122
|
+
```bash
|
|
123
|
+
cp .env.example .env.local
|
|
124
|
+
# Edit .env.local with your Gmail credentials and secret key
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
3. **Run the development server**:
|
|
128
|
+
```bash
|
|
129
|
+
npm run dev
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
4. **Open [http://localhost:3000](http://localhost:3000)** to see:
|
|
133
|
+
- Interactive OTP demo with email sending
|
|
134
|
+
- Live documentation with code examples
|
|
135
|
+
- All features working together
|
|
136
|
+
|
|
137
|
+
See `example/README.md` for more details.
|
|
138
|
+
|
|
139
|
+
## Publishing to NPM
|
|
140
|
+
|
|
141
|
+
1. **Login to NPM**:
|
|
142
|
+
```bash
|
|
143
|
+
npm login
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
2. **Build the package**:
|
|
147
|
+
```bash
|
|
148
|
+
npm run build
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
3. **Publish**:
|
|
152
|
+
```bash
|
|
153
|
+
npm publish --access public
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
ISC
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// src/react/components/input-otp.tsx
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { OTPInput, OTPInputContext } from "input-otp";
|
|
4
|
+
|
|
5
|
+
// src/utils/constants.ts
|
|
6
|
+
import { clsx } from "clsx";
|
|
7
|
+
import { twMerge } from "tailwind-merge";
|
|
8
|
+
function cn(...inputs) {
|
|
9
|
+
return twMerge(clsx(inputs));
|
|
10
|
+
}
|
|
11
|
+
var OTP_DEFAULTS = {
|
|
12
|
+
LENGTH: 6,
|
|
13
|
+
EXPIRY_MINUTES: 5,
|
|
14
|
+
RESEND_COOLDOWN_SECONDS: 120,
|
|
15
|
+
// 2 minutes
|
|
16
|
+
MAX_ATTEMPTS: 5,
|
|
17
|
+
RATE_LIMIT_RESET_HOURS: 1
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/react/components/input-otp.tsx
|
|
21
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
22
|
+
var Dot = () => /* @__PURE__ */ jsx(
|
|
23
|
+
"svg",
|
|
24
|
+
{
|
|
25
|
+
width: "15",
|
|
26
|
+
height: "15",
|
|
27
|
+
viewBox: "0 0 15 15",
|
|
28
|
+
fill: "none",
|
|
29
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
30
|
+
className: "h-4 w-4",
|
|
31
|
+
children: /* @__PURE__ */ jsx(
|
|
32
|
+
"path",
|
|
33
|
+
{
|
|
34
|
+
d: "M7.5 7.5C7.5 8.32843 6.82843 9 6 9C5.17157 9 4.5 8.32843 4.5 7.5C4.5 6.67157 5.17157 6 6 6C6.82843 6 7.5 6.67157 7.5 7.5Z",
|
|
35
|
+
fill: "currentColor",
|
|
36
|
+
fillRule: "evenodd",
|
|
37
|
+
clipRule: "evenodd"
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
var InputOTP = React.forwardRef(
|
|
43
|
+
({ className, containerClassName, ...props }, ref) => /* @__PURE__ */ jsx(
|
|
44
|
+
OTPInput,
|
|
45
|
+
{
|
|
46
|
+
ref,
|
|
47
|
+
containerClassName: cn("flex items-center gap-2 has-[:disabled]:opacity-50", containerClassName),
|
|
48
|
+
className: cn("disabled:cursor-not-allowed", className),
|
|
49
|
+
...props
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
);
|
|
53
|
+
InputOTP.displayName = "InputOTP";
|
|
54
|
+
var InputOTPGroup = React.forwardRef(
|
|
55
|
+
({ className, ...props }, ref) => /* @__PURE__ */ jsx("div", { ref, className: cn("flex items-center", className), ...props })
|
|
56
|
+
);
|
|
57
|
+
InputOTPGroup.displayName = "InputOTPGroup";
|
|
58
|
+
var InputOTPSlot = React.forwardRef(({ index, className, ...props }, ref) => {
|
|
59
|
+
const inputOTPContext = React.useContext(OTPInputContext);
|
|
60
|
+
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index];
|
|
61
|
+
return /* @__PURE__ */ jsxs(
|
|
62
|
+
"div",
|
|
63
|
+
{
|
|
64
|
+
ref,
|
|
65
|
+
className: cn(
|
|
66
|
+
"relative flex h-10 w-10 items-center justify-center border-y border-r border-input text-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md",
|
|
67
|
+
isActive && "z-10 ring-2 ring-ring ring-offset-background",
|
|
68
|
+
className
|
|
69
|
+
),
|
|
70
|
+
...props,
|
|
71
|
+
children: [
|
|
72
|
+
char,
|
|
73
|
+
hasFakeCaret && /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute inset-0 flex items-center justify-center", children: /* @__PURE__ */ jsx("div", { className: "h-4 w-px animate-caret-blink bg-foreground duration-1000" }) })
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
InputOTPSlot.displayName = "InputOTPSlot";
|
|
79
|
+
var InputOTPSeparator = React.forwardRef(
|
|
80
|
+
({ ...props }, ref) => /* @__PURE__ */ jsx("div", { ref, role: "separator", ...props, children: /* @__PURE__ */ jsx(Dot, {}) })
|
|
81
|
+
);
|
|
82
|
+
InputOTPSeparator.displayName = "InputOTPSeparator";
|
|
83
|
+
|
|
84
|
+
// src/react/hooks/use-otp.ts
|
|
85
|
+
import { useState, useEffect } from "react";
|
|
86
|
+
function useOTP({ email, onRateLimit }) {
|
|
87
|
+
const [otpTimeLeft, setOtpTimeLeft] = useState(OTP_DEFAULTS.EXPIRY_MINUTES * 60);
|
|
88
|
+
const [resendTimeLeft, setResendTimeLeft] = useState(0);
|
|
89
|
+
const [attemptsLeft, setAttemptsLeft] = useState(OTP_DEFAULTS.MAX_ATTEMPTS);
|
|
90
|
+
const [isRateLimited, setIsRateLimited] = useState(false);
|
|
91
|
+
const [rateLimitResetTime, setRateLimitResetTime] = useState(null);
|
|
92
|
+
const getStorageKey = (prefix) => `${prefix}_${email}`;
|
|
93
|
+
const getStoredData = (key) => {
|
|
94
|
+
if (typeof window === "undefined") return null;
|
|
95
|
+
const data = sessionStorage.getItem(key);
|
|
96
|
+
return data ? JSON.parse(data) : null;
|
|
97
|
+
};
|
|
98
|
+
const setStoredData = (key, data) => {
|
|
99
|
+
if (typeof window !== "undefined") {
|
|
100
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const removeStoredData = (key) => {
|
|
104
|
+
if (typeof window !== "undefined") {
|
|
105
|
+
sessionStorage.removeItem(key);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const checkRateLimit = () => {
|
|
109
|
+
const key = getStorageKey("rate_limit");
|
|
110
|
+
const data = getStoredData(key);
|
|
111
|
+
if (!data) {
|
|
112
|
+
setAttemptsLeft(OTP_DEFAULTS.MAX_ATTEMPTS);
|
|
113
|
+
setIsRateLimited(false);
|
|
114
|
+
return { limited: false, attemptsLeft: OTP_DEFAULTS.MAX_ATTEMPTS };
|
|
115
|
+
}
|
|
116
|
+
const currentTime = Date.now();
|
|
117
|
+
const resetTime = OTP_DEFAULTS.RATE_LIMIT_RESET_HOURS * 60 * 60 * 1e3;
|
|
118
|
+
if (currentTime - data.firstAttemptTime > resetTime) {
|
|
119
|
+
removeStoredData(key);
|
|
120
|
+
setAttemptsLeft(OTP_DEFAULTS.MAX_ATTEMPTS);
|
|
121
|
+
setIsRateLimited(false);
|
|
122
|
+
return { limited: false, attemptsLeft: OTP_DEFAULTS.MAX_ATTEMPTS };
|
|
123
|
+
}
|
|
124
|
+
const attempts = OTP_DEFAULTS.MAX_ATTEMPTS - data.attempts;
|
|
125
|
+
const limited = data.attempts >= OTP_DEFAULTS.MAX_ATTEMPTS;
|
|
126
|
+
const limitResetTime = data.firstAttemptTime + resetTime;
|
|
127
|
+
setAttemptsLeft(Math.max(0, attempts));
|
|
128
|
+
setIsRateLimited(limited);
|
|
129
|
+
setRateLimitResetTime(limited ? limitResetTime : null);
|
|
130
|
+
if (limited && onRateLimit) {
|
|
131
|
+
onRateLimit(limitResetTime);
|
|
132
|
+
}
|
|
133
|
+
return { limited, attemptsLeft: Math.max(0, attempts), resetTime: limited ? limitResetTime : void 0 };
|
|
134
|
+
};
|
|
135
|
+
const updateRateLimit = () => {
|
|
136
|
+
const key = getStorageKey("rate_limit");
|
|
137
|
+
const data = getStoredData(key);
|
|
138
|
+
const currentTime = Date.now();
|
|
139
|
+
let newData;
|
|
140
|
+
if (!data) {
|
|
141
|
+
newData = {
|
|
142
|
+
attempts: 1,
|
|
143
|
+
firstAttemptTime: currentTime,
|
|
144
|
+
lastAttemptTime: currentTime
|
|
145
|
+
};
|
|
146
|
+
} else {
|
|
147
|
+
const resetTime = OTP_DEFAULTS.RATE_LIMIT_RESET_HOURS * 60 * 60 * 1e3;
|
|
148
|
+
if (currentTime - data.firstAttemptTime > resetTime) {
|
|
149
|
+
newData = {
|
|
150
|
+
attempts: 1,
|
|
151
|
+
firstAttemptTime: currentTime,
|
|
152
|
+
lastAttemptTime: currentTime
|
|
153
|
+
};
|
|
154
|
+
} else {
|
|
155
|
+
newData = {
|
|
156
|
+
...data,
|
|
157
|
+
attempts: data.attempts + 1,
|
|
158
|
+
lastAttemptTime: currentTime
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
setStoredData(key, newData);
|
|
163
|
+
checkRateLimit();
|
|
164
|
+
return newData;
|
|
165
|
+
};
|
|
166
|
+
const checkResendCooldown = () => {
|
|
167
|
+
const key = getStorageKey("last_sent");
|
|
168
|
+
const lastSentTime = getStoredData(key);
|
|
169
|
+
if (!lastSentTime) {
|
|
170
|
+
setResendTimeLeft(0);
|
|
171
|
+
return { canResend: true, timeLeft: 0 };
|
|
172
|
+
}
|
|
173
|
+
const currentTime = Date.now();
|
|
174
|
+
const timeSinceLastSent = currentTime - lastSentTime;
|
|
175
|
+
const cooldown = OTP_DEFAULTS.RESEND_COOLDOWN_SECONDS * 1e3;
|
|
176
|
+
const canResend = timeSinceLastSent >= cooldown;
|
|
177
|
+
const timeLeft = canResend ? 0 : Math.ceil((cooldown - timeSinceLastSent) / 1e3);
|
|
178
|
+
setResendTimeLeft(timeLeft);
|
|
179
|
+
return { canResend, timeLeft };
|
|
180
|
+
};
|
|
181
|
+
const recordSentOTP = () => {
|
|
182
|
+
const key = getStorageKey("last_sent");
|
|
183
|
+
setStoredData(key, Date.now());
|
|
184
|
+
updateRateLimit();
|
|
185
|
+
setOtpTimeLeft(OTP_DEFAULTS.EXPIRY_MINUTES * 60);
|
|
186
|
+
setResendTimeLeft(OTP_DEFAULTS.RESEND_COOLDOWN_SECONDS);
|
|
187
|
+
};
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
checkRateLimit();
|
|
190
|
+
checkResendCooldown();
|
|
191
|
+
const timer = setInterval(() => {
|
|
192
|
+
setOtpTimeLeft((prev) => Math.max(0, prev - 1));
|
|
193
|
+
const { timeLeft } = checkResendCooldown();
|
|
194
|
+
setResendTimeLeft(timeLeft);
|
|
195
|
+
}, 1e3);
|
|
196
|
+
return () => clearInterval(timer);
|
|
197
|
+
}, [email]);
|
|
198
|
+
const formatTime = (seconds) => {
|
|
199
|
+
const mins = Math.floor(seconds / 60);
|
|
200
|
+
const secs = seconds % 60;
|
|
201
|
+
return `${mins}:${secs.toString().padStart(2, "0")}`;
|
|
202
|
+
};
|
|
203
|
+
return {
|
|
204
|
+
otpTimeLeft,
|
|
205
|
+
resendTimeLeft,
|
|
206
|
+
attemptsLeft,
|
|
207
|
+
isRateLimited,
|
|
208
|
+
rateLimitResetTime,
|
|
209
|
+
formatTime,
|
|
210
|
+
recordSentOTP,
|
|
211
|
+
checkRateLimit
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export {
|
|
216
|
+
cn,
|
|
217
|
+
OTP_DEFAULTS,
|
|
218
|
+
InputOTP,
|
|
219
|
+
InputOTPGroup,
|
|
220
|
+
InputOTPSlot,
|
|
221
|
+
InputOTPSeparator,
|
|
222
|
+
useOTP
|
|
223
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
__publicField
|
|
7
|
+
};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "./chunk-QZ7TP4HQ.mjs";
|
|
4
|
+
|
|
5
|
+
// src/server/email.ts
|
|
6
|
+
import nodemailer from "nodemailer";
|
|
7
|
+
var EmailService = class {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
__publicField(this, "transporter");
|
|
10
|
+
__publicField(this, "config");
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.transporter = nodemailer.createTransport(config.transport);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Verify SMTP connection configuration
|
|
16
|
+
*/
|
|
17
|
+
async verifyConnection() {
|
|
18
|
+
try {
|
|
19
|
+
await this.transporter.verify();
|
|
20
|
+
return true;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error("SMTP connection verification failed:", error);
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Send an email
|
|
28
|
+
* @param options - Email options (to, subject, html, text, from)
|
|
29
|
+
* @returns - Result of the send operation
|
|
30
|
+
*/
|
|
31
|
+
async sendEmail(options) {
|
|
32
|
+
const from = options.from || this.config.defaults?.from;
|
|
33
|
+
if (!from) {
|
|
34
|
+
throw new Error("From address is required either in options or config defaults");
|
|
35
|
+
}
|
|
36
|
+
const mailOptions = {
|
|
37
|
+
from: {
|
|
38
|
+
name: from.name,
|
|
39
|
+
address: from.address
|
|
40
|
+
},
|
|
41
|
+
to: options.to,
|
|
42
|
+
subject: options.subject,
|
|
43
|
+
html: options.html,
|
|
44
|
+
text: options.text || options.html.replace(/<[^>]*>/g, "")
|
|
45
|
+
// Simple HTML to text fallback
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
const info = await this.transporter.sendMail(mailOptions);
|
|
49
|
+
return {
|
|
50
|
+
success: true,
|
|
51
|
+
messageId: info.messageId
|
|
52
|
+
};
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error("Error sending email:", error);
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/server/crypto.ts
|
|
61
|
+
import crypto from "crypto";
|
|
62
|
+
var ALGORITHM = "aes-256-cbc";
|
|
63
|
+
function encrypt(text, secretKey) {
|
|
64
|
+
try {
|
|
65
|
+
const key = Buffer.from(secretKey.padEnd(32).slice(0, 32));
|
|
66
|
+
const iv = crypto.randomBytes(16);
|
|
67
|
+
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
|
|
68
|
+
let encrypted = cipher.update(text, "utf8", "base64");
|
|
69
|
+
encrypted += cipher.final("base64");
|
|
70
|
+
return iv.toString("hex") + ":" + encrypted;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error("Encryption error:", error);
|
|
73
|
+
throw new Error("Failed to encrypt data");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function decrypt(encryptedText, secretKey) {
|
|
77
|
+
try {
|
|
78
|
+
const key = Buffer.from(secretKey.padEnd(32).slice(0, 32));
|
|
79
|
+
const parts = encryptedText.split(":");
|
|
80
|
+
if (parts.length !== 2) {
|
|
81
|
+
throw new Error("Invalid encrypted text format");
|
|
82
|
+
}
|
|
83
|
+
const iv = Buffer.from(parts[0], "hex");
|
|
84
|
+
const encrypted = parts[1];
|
|
85
|
+
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
|
|
86
|
+
let decrypted = decipher.update(encrypted, "base64", "utf8");
|
|
87
|
+
decrypted += decipher.final("utf8");
|
|
88
|
+
return decrypted;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error("Decryption error:", error);
|
|
91
|
+
throw new Error("Failed to decrypt data");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/server/otp.ts
|
|
96
|
+
var OTPService = class {
|
|
97
|
+
constructor(secretKey) {
|
|
98
|
+
__publicField(this, "secretKey");
|
|
99
|
+
if (!secretKey) {
|
|
100
|
+
throw new Error("OTPService: secretKey is required");
|
|
101
|
+
}
|
|
102
|
+
this.secretKey = secretKey;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Generate a numeric OTP of specified length
|
|
106
|
+
* @param length - Length of the OTP (default: 6)
|
|
107
|
+
* @returns - The generated OTP string
|
|
108
|
+
*/
|
|
109
|
+
generateOTP(length = 6) {
|
|
110
|
+
const min = Math.pow(10, length - 1);
|
|
111
|
+
const max = Math.pow(10, length) - 1;
|
|
112
|
+
return Math.floor(min + Math.random() * (max - min + 1)).toString();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Encrypt OTP data for secure storage/transmission
|
|
116
|
+
* @param otp - The OTP string
|
|
117
|
+
* @param timestamp - The timestamp when OTP was generated (default: now)
|
|
118
|
+
* @returns - Encrypted OTP data string
|
|
119
|
+
*/
|
|
120
|
+
encryptOTP(otp, timestamp = Date.now()) {
|
|
121
|
+
const data = JSON.stringify({ otp, timestamp });
|
|
122
|
+
return encrypt(data, this.secretKey);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Decrypt OTP data
|
|
126
|
+
* @param encryptedData - The encrypted OTP string
|
|
127
|
+
* @returns - Decrypted OTP data object or null if failed
|
|
128
|
+
*/
|
|
129
|
+
decryptOTP(encryptedData) {
|
|
130
|
+
try {
|
|
131
|
+
const decryptedData = decrypt(encryptedData, this.secretKey);
|
|
132
|
+
return JSON.parse(decryptedData);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error("Error decrypting OTP data:", error);
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Verify an OTP against encrypted data
|
|
140
|
+
* @param inputOtp - The OTP provided by the user
|
|
141
|
+
* @param encryptedData - The encrypted OTP data
|
|
142
|
+
* @param expiryMinutes - Expiry time in minutes (default: 5)
|
|
143
|
+
* @returns - Object containing valid status and message
|
|
144
|
+
*/
|
|
145
|
+
verifyOTP(inputOtp, encryptedData, expiryMinutes = 5) {
|
|
146
|
+
const data = this.decryptOTP(encryptedData);
|
|
147
|
+
if (!data) {
|
|
148
|
+
return { valid: false, message: "Invalid OTP data" };
|
|
149
|
+
}
|
|
150
|
+
const { otp, timestamp } = data;
|
|
151
|
+
const currentTime = Date.now();
|
|
152
|
+
const otpAge = currentTime - timestamp;
|
|
153
|
+
const expiryTime = expiryMinutes * 60 * 1e3;
|
|
154
|
+
if (otpAge > expiryTime) {
|
|
155
|
+
return { valid: false, message: "OTP has expired" };
|
|
156
|
+
}
|
|
157
|
+
if (otp !== inputOtp) {
|
|
158
|
+
return { valid: false, message: "Incorrect OTP" };
|
|
159
|
+
}
|
|
160
|
+
return { valid: true, message: "OTP verified successfully" };
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// src/server/index.ts
|
|
165
|
+
var IrisMailService = class {
|
|
166
|
+
constructor(config) {
|
|
167
|
+
__publicField(this, "email");
|
|
168
|
+
__publicField(this, "otp");
|
|
169
|
+
this.email = new EmailService(config.email);
|
|
170
|
+
this.otp = new OTPService(config.secretKey);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export {
|
|
175
|
+
EmailService,
|
|
176
|
+
encrypt,
|
|
177
|
+
decrypt,
|
|
178
|
+
OTPService,
|
|
179
|
+
IrisMailService
|
|
180
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { EmailConfig, EmailService, IrisMailService, OTPData, OTPService, SendEmailOptions, decrypt, encrypt } from './server/index.mjs';
|
|
2
|
+
export { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, useOTP } from './react/index.mjs';
|
|
3
|
+
import { ClassValue } from 'clsx';
|
|
4
|
+
import 'input-otp';
|
|
5
|
+
import 'react';
|
|
6
|
+
|
|
7
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
8
|
+
declare const OTP_DEFAULTS: {
|
|
9
|
+
LENGTH: number;
|
|
10
|
+
EXPIRY_MINUTES: number;
|
|
11
|
+
RESEND_COOLDOWN_SECONDS: number;
|
|
12
|
+
MAX_ATTEMPTS: number;
|
|
13
|
+
RATE_LIMIT_RESET_HOURS: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { OTP_DEFAULTS, cn };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { EmailConfig, EmailService, IrisMailService, OTPData, OTPService, SendEmailOptions, decrypt, encrypt } from './server/index.js';
|
|
2
|
+
export { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, useOTP } from './react/index.js';
|
|
3
|
+
import { ClassValue } from 'clsx';
|
|
4
|
+
import 'input-otp';
|
|
5
|
+
import 'react';
|
|
6
|
+
|
|
7
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
8
|
+
declare const OTP_DEFAULTS: {
|
|
9
|
+
LENGTH: number;
|
|
10
|
+
EXPIRY_MINUTES: number;
|
|
11
|
+
RESEND_COOLDOWN_SECONDS: number;
|
|
12
|
+
MAX_ATTEMPTS: number;
|
|
13
|
+
RATE_LIMIT_RESET_HOURS: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { OTP_DEFAULTS, cn };
|