samirhosninet commited on
Commit
6be5db6
·
1 Parent(s): 587459d

chore: remove JSON fallback and make registrations strictly MySQL-only

Browse files
app/actions/waitlist.ts CHANGED
@@ -1,13 +1,9 @@
1
  "use server";
2
 
3
- import fs from "fs";
4
- import path from "path";
5
  import { headers } from "next/headers";
6
  import crypto from "crypto";
7
  import mysql from "mysql2/promise";
8
 
9
- const REGISTRATIONS_FILE = path.join(process.cwd(), "data", "waitlist-registrations.json");
10
-
11
  export interface RegistrationResult {
12
  success: boolean;
13
  error?: string;
@@ -98,147 +94,138 @@ async function sendWelcomeEmail(email: string, ticketNo: string) {
98
  <title>HUBAPI ACCESS PASS APPROVED</title>
99
  <style>
100
  body {
101
- background-color: #09090b;
102
- color: #e4e4e7;
103
- font-family: monospace;
 
104
  margin: 0;
105
- padding: 40px 20px;
106
  }
107
  .container {
108
  max-width: 600px;
109
  margin: 0 auto;
110
- background-color: #020202;
111
- border: 1px solid #f59e0b;
112
- border-radius: 8px;
113
- padding: 30px;
114
- box-shadow: 0 0 30px rgba(245, 158, 11, 0.15);
115
  }
116
  .header {
117
- border-bottom: 1px solid #27272a;
118
  padding-bottom: 20px;
119
- margin-bottom: 25px;
120
  text-align: center;
121
  }
122
  .logo {
123
- color: #fbbf24;
124
- font-size: 28px;
125
- font-weight: bold;
126
- letter-spacing: 0.15em;
 
127
  }
128
  .title {
129
- font-size: 18px;
130
- color: #ffffff;
131
- margin-top: 10px;
132
- text-transform: uppercase;
133
- letter-spacing: 0.05em;
134
- }
135
- .badge {
136
- background-color: rgba(245, 158, 11, 0.1);
137
- border: 1px solid rgba(245, 158, 11, 0.3);
138
- color: #fbbf24;
139
- display: inline-block;
140
- padding: 6px 12px;
141
- font-size: 11px;
142
- font-weight: bold;
143
  text-transform: uppercase;
144
- margin-bottom: 20px;
 
145
  }
146
- .ticket-box {
147
- border: 1px dashed rgba(255, 255, 255, 0.15);
148
- background-color: rgba(255, 255, 255, 0.02);
 
149
  padding: 20px;
150
- border-radius: 6px;
151
- margin: 25px 0;
152
- text-align: center;
153
- }
154
- .ticket-number {
155
- font-size: 38px;
156
- font-weight: bold;
157
- color: #ffffff;
158
- margin: 10px 0;
159
- letter-spacing: 0.05em;
160
  }
161
- .code-box {
162
- background-color: #09090b;
163
- border: 1px solid #27272a;
164
- padding: 10px;
165
  font-size: 12px;
166
- color: #fbbf24;
167
- word-break: break-all;
168
- margin-top: 8px;
169
  }
170
- .info-text {
171
- font-size: 12px;
172
- line-height: 1.6;
173
- color: #a1a1aa;
 
 
 
174
  }
175
  .btn {
176
  display: inline-block;
177
- background-color: #fbbf24;
178
- color: #000000;
179
  text-decoration: none;
180
  padding: 12px 24px;
181
  font-weight: bold;
182
  font-size: 12px;
183
  text-transform: uppercase;
184
  letter-spacing: 0.1em;
185
- margin: 20px 0;
186
- border-radius: 4px;
 
187
  }
188
  .footer {
189
- border-top: 1px solid #27272a;
190
- padding-top: 20px;
191
- margin-top: 30px;
192
- font-size: 9px;
193
- color: #52525b;
194
  text-align: center;
195
- line-height: 1.5;
196
  }
197
  </style>
198
  </head>
199
  <body>
200
  <div class="container">
201
  <div class="header">
202
- <div class="logo">CPU_HUBAPI</div>
203
- <div class="title">Neural Grid Entrance Approved</div>
204
- </div>
205
-
206
- <div style="text-align: center;">
207
- <div class="badge">SECURE_ONBOARDING // PASS</div>
208
  </div>
209
 
210
- <p class="info-text">
211
- Visitor identification verified. Your registration has been registered into the Neural Grid Gateway waitlist.
 
 
212
  </p>
213
 
214
- <div class="ticket-box">
215
- <div style="font-size: 10px; color: #71717a; text-transform: uppercase;">Allocated Grid Position</div>
216
- <div class="ticket-number">#${cleanTicketNo}</div>
217
- <div style="font-size: 10px; color: #71717a; text-transform: uppercase; margin-top: 15px;">Secure Access Token</div>
218
- <div class="code-box">${accessCode}</div>
 
 
 
 
 
 
 
 
 
 
 
 
219
  </div>
220
 
221
- <p class="info-text">
222
- Want to move up in the queue? Share your unique referral link with other developers. For every colleague who joins via your link, your system priority rank will advance:
223
  </p>
224
 
225
- <div style="text-align: center; margin: 15px 0;">
226
- <a href="${referralLink}" class="btn">Share referral Grid Link</a>
227
  </div>
228
 
229
- <p class="info-text" style="font-size: 11px;">
230
- <strong>Referral URL:</strong><br>
231
- <a href="${referralLink}" style="color: #fbbf24; text-decoration: none;">${referralLink}</a>
232
- </p>
233
-
234
- <p class="info-text" style="margin-top: 25px;">
235
- Welcome to the high-performance routing mesh.
236
- </p>
237
 
238
  <div class="footer">
239
- HUBAPI NEURAL EDGE MESH GATEWAY PROTOCOLS<br>
240
- This is a simulated marketing notice for early developer registrations.<br>
241
- © 2026 HUBAPI. All rights reserved.
242
  </div>
243
  </div>
244
  </body>
@@ -255,17 +242,17 @@ async function sendWelcomeEmail(email: string, ticketNo: string) {
255
  body: JSON.stringify({
256
  from: "HUBAPI <onboarding@resend.dev>",
257
  to: email,
258
- subject: `HUBAPI Access Granted // Ticket #${cleanTicketNo}`,
259
  html: emailHtml,
260
  }),
261
  });
262
 
263
- if (res.ok) {
264
- console.log("Welcome email sent successfully via Resend API to:", email);
265
- } else {
266
  const errText = await res.text();
267
- console.error("Resend API response error:", errText);
268
  }
 
 
269
  } catch (err) {
270
  console.error("Failed to send welcome email via Resend:", err);
271
  }
@@ -285,154 +272,80 @@ export async function registerWaitlist(formData: FormData): Promise<Registration
285
  return { success: false, error: "Please enter a valid email address." };
286
  }
287
 
 
 
 
 
 
 
 
288
  const headerList = await headers();
289
  const rawIp = headerList.get("x-forwarded-for")?.split(",")[0] || headerList.get("x-real-ip") || "127.0.0.1";
290
  const ipHash = crypto.createHash("sha256").update(rawIp).digest("hex");
291
  const ticketNo = getTicketNumber(email);
292
 
293
- // 1. MySQL Execution Flow (Primary if Pool exists)
294
- if (pool) {
295
- let connection: mysql.PoolConnection | null = null;
296
- try {
297
- connection = await pool.getConnection();
298
- await ensureMySQLTable(connection);
299
-
300
- // Check if email already registered
301
- const [emailCheck] = await connection.query<mysql.RowDataPacket[]>(
302
- "SELECT id FROM waitlist WHERE LOWER(email) = LOWER(?) LIMIT 1",
303
- [email]
304
- );
305
-
306
- if (emailCheck.length > 0) {
307
- connection.release();
308
- return { success: false, error: "This email is already registered on the waitlist." };
309
- }
310
-
311
- // Rate Limiting checks
312
- const [ipCheck] = await connection.query<mysql.RowDataPacket[]>(
313
- "SELECT timestamp FROM waitlist WHERE ip_hash = ? ORDER BY timestamp DESC",
314
- [ipHash]
315
- );
316
-
317
- const now = Date.now();
318
- const tooFast = ipCheck.some((row) => {
319
- const diff = now - new Date(row.timestamp).getTime();
320
- return diff < 60000; // 60 seconds
321
- });
322
-
323
- if (tooFast) {
324
- connection.release();
325
- return { success: false, error: "Please wait 60 seconds before submitting another request." };
326
- }
327
-
328
- if (ipCheck.length >= 5) {
329
- connection.release();
330
- return { success: false, error: "Limit reached. You have reached the maximum number of registrations allowed from this device." };
331
- }
332
-
333
- // Insert new registration
334
- const timestamp = new Date().toISOString();
335
- await connection.query(
336
- "INSERT INTO waitlist (email, timestamp, ip_hash, variant) VALUES (?, ?, ?, ?)",
337
- [email, timestamp, ipHash, variant]
338
- );
339
-
340
- connection.release();
341
- console.log(`[MySQL] Registered waitlist email: ${email}`);
342
-
343
- // Dispatch welcome email asynchronously via Resend
344
- sendWelcomeEmail(email, ticketNo);
345
-
346
- return { success: true };
347
- } catch (dbErr) {
348
- console.error("[MySQL] Error occurred, falling back to local JSON:", dbErr);
349
- if (connection) {
350
- try {
351
- connection.release();
352
- } catch {}
353
- }
354
- // Fall through to JSON storage
355
- }
356
- }
357
-
358
- interface LocalRegistration {
359
- email: string;
360
- timestamp: string;
361
- ipHash: string;
362
- variant: string;
363
- }
364
-
365
- // 2. JSON Fallback Execution Flow
366
  try {
367
- // Ensure data directory exists
368
- const dataDir = path.dirname(REGISTRATIONS_FILE);
369
- if (!fs.existsSync(dataDir)) {
370
- fs.mkdirSync(dataDir, { recursive: true });
371
- }
372
-
373
- let registrations: LocalRegistration[] = [];
374
-
375
- if (fs.existsSync(REGISTRATIONS_FILE)) {
376
- const fileData = fs.readFileSync(REGISTRATIONS_FILE, "utf-8");
377
- try {
378
- registrations = JSON.parse(fileData);
379
- if (!Array.isArray(registrations)) {
380
- registrations = [];
381
- }
382
- } catch {
383
- registrations = [];
384
- }
385
- }
386
 
387
  // Check if email already registered
388
- const exists = registrations.some(
389
- (reg) => reg.email.toLowerCase() === email.toLowerCase()
 
390
  );
391
 
392
- if (exists) {
 
393
  return { success: false, error: "This email is already registered on the waitlist." };
394
  }
395
 
396
- // Rate Limiting (F9)
397
- const userRegistrations = registrations.filter((reg) => reg.ipHash === ipHash);
 
 
 
398
 
399
- // Limit 1 submission per 60 seconds
400
  const now = Date.now();
401
- const tooFast = userRegistrations.some((reg) => {
402
- const diff = now - new Date(reg.timestamp).getTime();
403
  return diff < 60000; // 60 seconds
404
  });
405
 
406
  if (tooFast) {
 
407
  return { success: false, error: "Please wait 60 seconds before submitting another request." };
408
  }
409
 
410
- // Limit absolute total to 5 registrations per hashed IP (protect JSON from bloating)
411
- if (userRegistrations.length >= 5) {
412
  return { success: false, error: "Limit reached. You have reached the maximum number of registrations allowed from this device." };
413
  }
414
 
415
- // Add new registration
416
- registrations.push({
417
- email,
418
- timestamp: new Date().toISOString(),
419
- ipHash,
420
- variant,
421
- });
422
 
423
- fs.writeFileSync(REGISTRATIONS_FILE, JSON.stringify(registrations, null, 2), "utf-8");
424
- console.log(`[JSON] Registered waitlist email: ${email}`);
425
 
426
  // Dispatch welcome email asynchronously via Resend
427
  sendWelcomeEmail(email, ticketNo);
428
 
429
  return { success: true };
430
- } catch (err) {
431
- console.error("[JSON] Waitlist storage error:", err);
 
 
 
 
 
432
  return {
433
  success: false,
434
- error: "Internal server error occurred. Please try again later.",
435
  };
436
  }
437
  }
438
-
 
1
  "use server";
2
 
 
 
3
  import { headers } from "next/headers";
4
  import crypto from "crypto";
5
  import mysql from "mysql2/promise";
6
 
 
 
7
  export interface RegistrationResult {
8
  success: boolean;
9
  error?: string;
 
94
  <title>HUBAPI ACCESS PASS APPROVED</title>
95
  <style>
96
  body {
97
+ background-color: #030303;
98
+ color: #f5f5f5;
99
+ font-family: 'Courier New', Courier, monospace;
100
+ padding: 24px;
101
  margin: 0;
 
102
  }
103
  .container {
104
  max-width: 600px;
105
  margin: 0 auto;
106
+ border: 1px solid #1a1a1a;
107
+ background-color: #070708;
108
+ padding: 32px;
109
+ border-radius: 4px;
110
+ box-shadow: 0 4px 20px rgba(0,0,0,0.8);
111
  }
112
  .header {
113
+ border-bottom: 1px solid #222;
114
  padding-bottom: 20px;
115
+ margin-bottom: 24px;
116
  text-align: center;
117
  }
118
  .logo {
119
+ font-size: 24px;
120
+ font-weight: 900;
121
+ letter-spacing: 0.3em;
122
+ color: #ff9d00;
123
+ margin: 0;
124
  }
125
  .title {
126
+ font-size: 14px;
127
+ color: #888;
 
 
 
 
 
 
 
 
 
 
 
 
128
  text-transform: uppercase;
129
+ letter-spacing: 0.15em;
130
+ margin-top: 6px;
131
  }
132
+ .pass-card {
133
+ border: 1px dashed #ff9d00;
134
+ background: rgba(255, 157, 0, 0.03);
135
+ border-radius: 4px;
136
  padding: 20px;
137
+ margin: 24px 0;
138
+ font-family: monospace;
 
 
 
 
 
 
 
 
139
  }
140
+ .field {
141
+ margin-bottom: 12px;
142
+ display: flex;
143
+ justify-content: space-between;
144
  font-size: 12px;
145
+ border-bottom: 1px dotted #1a1a1a;
146
+ padding-bottom: 4px;
 
147
  }
148
+ .field-label {
149
+ color: #666;
150
+ text-transform: uppercase;
151
+ }
152
+ .field-val {
153
+ color: #ff9d00;
154
+ font-weight: bold;
155
  }
156
  .btn {
157
  display: inline-block;
158
+ background-color: #ff9d00;
159
+ color: #000;
160
  text-decoration: none;
161
  padding: 12px 24px;
162
  font-weight: bold;
163
  font-size: 12px;
164
  text-transform: uppercase;
165
  letter-spacing: 0.1em;
166
+ border-radius: 2px;
167
+ text-align: center;
168
+ margin-top: 10px;
169
  }
170
  .footer {
171
+ margin-top: 32px;
172
+ border-top: 1px solid #1a1a1a;
173
+ padding-top: 16px;
174
+ font-size: 10px;
175
+ color: #444;
176
  text-align: center;
177
+ line-height: 1.6;
178
  }
179
  </style>
180
  </head>
181
  <body>
182
  <div class="container">
183
  <div class="header">
184
+ <h1 class="logo">HUBAPI</h1>
185
+ <div class="title">Waitlist Confirmation Voucher</div>
 
 
 
 
186
  </div>
187
 
188
+ <p style="font-size: 13px; line-height: 1.6; color: #ccc;">
189
+ Greetings Agent,
190
+ <br><br>
191
+ Your neural synchronization handshake with Hostinger gateways was successful. You have officially secured a node position on the HUBAPI waitlist. Your verified cryptographic ticket has been registered on the distributed ledger.
192
  </p>
193
 
194
+ <div class="pass-card">
195
+ <div class="field">
196
+ <span class="field-label">Registry Node</span>
197
+ <span class="field-val">HUBAPI-PROD-01</span>
198
+ </div>
199
+ <div class="field">
200
+ <span class="field-label">Assigned Ticket</span>
201
+ <span class="field-val">#${cleanTicketNo}</span>
202
+ </div>
203
+ <div class="field">
204
+ <span class="field-label">Access Token</span>
205
+ <span class="field-val" style="font-size: 10px;">${accessCode}</span>
206
+ </div>
207
+ <div class="field" style="border: none; margin: 0; padding: 0;">
208
+ <span class="field-label">System Priority</span>
209
+ <span class="field-val" style="color: #00ff66;">EARLY_ADOPTER_NOMINAL</span>
210
+ </div>
211
  </div>
212
 
213
+ <p style="font-size: 12px; line-height: 1.5; color: #888;">
214
+ Refer your network peers to bypass standard throttling gates and secure priority allocation metrics:
215
  </p>
216
 
217
+ <div style="background-color: #09090b; padding: 12px; border: 1px solid #141417; border-radius: 2px; font-size: 11px; margin-bottom: 20px; word-break: break-all; text-align: center;">
218
+ <a href="${referralLink}" style="color: #ff9d00; text-decoration: none;">${referralLink}</a>
219
  </div>
220
 
221
+ <div style="text-align: center;">
222
+ <a href="${referralLink}" class="btn">Elevate Access Node</a>
223
+ </div>
 
 
 
 
 
224
 
225
  <div class="footer">
226
+ This is an automated encrypted transmission. Response is unmonitored.
227
+ <br>
228
+ © 2026 HUBAPI. Edge Gateways Active. Secure Connection Guaranteed.
229
  </div>
230
  </div>
231
  </body>
 
242
  body: JSON.stringify({
243
  from: "HUBAPI <onboarding@resend.dev>",
244
  to: email,
245
+ subject: `[HUBAPI] SECURE TICKET VERIFIED - #${ticketNo}`,
246
  html: emailHtml,
247
  }),
248
  });
249
 
250
+ if (!res.ok) {
 
 
251
  const errText = await res.text();
252
+ throw new Error(`Resend API returned status ${res.status}: ${errText}`);
253
  }
254
+
255
+ console.log(`[Resend] Successfully dispatched welcome email to ${email}`);
256
  } catch (err) {
257
  console.error("Failed to send welcome email via Resend:", err);
258
  }
 
272
  return { success: false, error: "Please enter a valid email address." };
273
  }
274
 
275
+ if (!pool) {
276
+ return {
277
+ success: false,
278
+ error: "Database configuration is missing. Registrations are currently disabled.",
279
+ };
280
+ }
281
+
282
  const headerList = await headers();
283
  const rawIp = headerList.get("x-forwarded-for")?.split(",")[0] || headerList.get("x-real-ip") || "127.0.0.1";
284
  const ipHash = crypto.createHash("sha256").update(rawIp).digest("hex");
285
  const ticketNo = getTicketNumber(email);
286
 
287
+ let connection: mysql.PoolConnection | null = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  try {
289
+ connection = await pool.getConnection();
290
+ await ensureMySQLTable(connection);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
  // Check if email already registered
293
+ const [emailCheck] = await connection.query<mysql.RowDataPacket[]>(
294
+ "SELECT id FROM waitlist WHERE LOWER(email) = LOWER(?) LIMIT 1",
295
+ [email]
296
  );
297
 
298
+ if (emailCheck.length > 0) {
299
+ connection.release();
300
  return { success: false, error: "This email is already registered on the waitlist." };
301
  }
302
 
303
+ // Rate Limiting checks
304
+ const [ipCheck] = await connection.query<mysql.RowDataPacket[]>(
305
+ "SELECT timestamp FROM waitlist WHERE ip_hash = ? ORDER BY timestamp DESC",
306
+ [ipHash]
307
+ );
308
 
 
309
  const now = Date.now();
310
+ const tooFast = ipCheck.some((row) => {
311
+ const diff = now - new Date(row.timestamp).getTime();
312
  return diff < 60000; // 60 seconds
313
  });
314
 
315
  if (tooFast) {
316
+ connection.release();
317
  return { success: false, error: "Please wait 60 seconds before submitting another request." };
318
  }
319
 
320
+ if (ipCheck.length >= 5) {
321
+ connection.release();
322
  return { success: false, error: "Limit reached. You have reached the maximum number of registrations allowed from this device." };
323
  }
324
 
325
+ // Insert new registration
326
+ const timestamp = new Date().toISOString();
327
+ await connection.query(
328
+ "INSERT INTO waitlist (email, timestamp, ip_hash, variant) VALUES (?, ?, ?, ?)",
329
+ [email, timestamp, ipHash, variant]
330
+ );
 
331
 
332
+ connection.release();
333
+ console.log(`[MySQL] Registered waitlist email: ${email}`);
334
 
335
  // Dispatch welcome email asynchronously via Resend
336
  sendWelcomeEmail(email, ticketNo);
337
 
338
  return { success: true };
339
+ } catch (dbErr) {
340
+ console.error("[MySQL] Database registration error:", dbErr);
341
+ if (connection) {
342
+ try {
343
+ connection.release();
344
+ } catch {}
345
+ }
346
  return {
347
  success: false,
348
+ error: "Failed to save registration to the database. Please try again later.",
349
  };
350
  }
351
  }
 
data/waitlist-registrations.json DELETED
@@ -1,18 +0,0 @@
1
- [
2
- {
3
- "email": "dr.samirhosni@gmail.com",
4
- "timestamp": "2026-05-20T19:55:13.044Z",
5
- "ipHash": "eff8e7ca506627fe15dda5e0e512fcaad70b6d520f37cc76597fdb4f2d83a1a3"
6
- },
7
- {
8
- "email": "seomax.social@gmail.com",
9
- "timestamp": "2026-05-20T20:19:17.490Z",
10
- "ipHash": "eff8e7ca506627fe15dda5e0e512fcaad70b6d520f37cc76597fdb4f2d83a1a3"
11
- },
12
- {
13
- "email": "dr.samirhosni@hotmail.com",
14
- "timestamp": "2026-05-20T22:00:45.811Z",
15
- "ipHash": "eff8e7ca506627fe15dda5e0e512fcaad70b6d520f37cc76597fdb4f2d83a1a3",
16
- "variant": "classic"
17
- }
18
- ]