sitepaige-mcp-server 1.0.0 → 1.0.3
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/components/form.tsx +133 -6
- package/components/login.tsx +249 -21
- package/components/menu.tsx +128 -3
- package/components/testimonial.tsx +1 -1
- package/defaultapp/api/Auth/resend-verification/route.ts +130 -0
- package/defaultapp/api/Auth/route.ts +105 -13
- package/defaultapp/api/Auth/signup/route.ts +133 -0
- package/defaultapp/api/Auth/verify-email/route.ts +105 -0
- package/defaultapp/db-password-auth.ts +362 -0
- package/defaultapp/db-users.ts +11 -11
- package/defaultapp/middleware.ts +15 -17
- package/defaultapp/storage/email.ts +162 -0
- package/dist/blueprintWriter.js +15 -1
- package/dist/blueprintWriter.js.map +1 -1
- package/dist/components/form.tsx +133 -6
- package/dist/components/login.tsx +249 -21
- package/dist/components/menu.tsx +128 -3
- package/dist/components/testimonial.tsx +1 -1
- package/dist/defaultapp/api/Auth/resend-verification/route.ts +130 -0
- package/dist/defaultapp/api/Auth/route.ts +105 -13
- package/dist/defaultapp/api/Auth/signup/route.ts +133 -0
- package/dist/defaultapp/api/Auth/verify-email/route.ts +105 -0
- package/dist/defaultapp/db-password-auth.ts +362 -0
- package/dist/defaultapp/db-users.ts +11 -11
- package/dist/defaultapp/middleware.ts +15 -17
- package/dist/defaultapp/storage/email.ts +162 -0
- package/dist/generators/apis.js +1 -0
- package/dist/generators/apis.js.map +1 -1
- package/dist/generators/defaultapp.js +2 -2
- package/dist/generators/defaultapp.js.map +1 -1
- package/dist/generators/env-example-template.txt +27 -0
- package/dist/generators/images.js +38 -13
- package/dist/generators/images.js.map +1 -1
- package/dist/generators/pages.js +1 -1
- package/dist/generators/pages.js.map +1 -1
- package/dist/generators/sql.js +19 -0
- package/dist/generators/sql.js.map +1 -1
- package/dist/generators/views.js +29 -14
- package/dist/generators/views.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Username/Password Authentication Database Utilities
|
|
3
|
+
* Handles all database operations related to username/password authentication
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { db_init, db_query } from './db';
|
|
7
|
+
import type { DatabaseClient } from './db';
|
|
8
|
+
import * as crypto from 'node:crypto';
|
|
9
|
+
import { scrypt, randomBytes } from 'node:crypto';
|
|
10
|
+
import { promisify } from 'node:util';
|
|
11
|
+
|
|
12
|
+
const scryptAsync = promisify(scrypt);
|
|
13
|
+
|
|
14
|
+
export interface PasswordAuth {
|
|
15
|
+
id: string;
|
|
16
|
+
email: string; // Email serves as username
|
|
17
|
+
passwordhash: string;
|
|
18
|
+
salt: string;
|
|
19
|
+
verificationtoken?: string;
|
|
20
|
+
verificationtokenexpires?: string;
|
|
21
|
+
emailverified: boolean;
|
|
22
|
+
resettoken?: string;
|
|
23
|
+
resettokenexpires?: string;
|
|
24
|
+
createdat: string;
|
|
25
|
+
updatedat: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Hash a password using scrypt
|
|
30
|
+
*/
|
|
31
|
+
export async function hashPassword(password: string): Promise<{ hash: string; salt: string }> {
|
|
32
|
+
const salt = randomBytes(32).toString('base64');
|
|
33
|
+
const hash = (await scryptAsync(password, salt, 64)) as Buffer;
|
|
34
|
+
return {
|
|
35
|
+
hash: hash.toString('base64'),
|
|
36
|
+
salt
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Verify a password against its hash
|
|
42
|
+
*/
|
|
43
|
+
export async function verifyPassword(password: string, hash: string, salt: string): Promise<boolean> {
|
|
44
|
+
const derivedKey = (await scryptAsync(password, salt, 64)) as Buffer;
|
|
45
|
+
return derivedKey.toString('base64') === hash;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create the password authentication table
|
|
50
|
+
*/
|
|
51
|
+
export async function createPasswordAuthTable(): Promise<void> {
|
|
52
|
+
const client = await db_init();
|
|
53
|
+
|
|
54
|
+
const createTableQuery = `
|
|
55
|
+
CREATE TABLE IF NOT EXISTS passwordauth (
|
|
56
|
+
id TEXT PRIMARY KEY,
|
|
57
|
+
email TEXT NOT NULL UNIQUE,
|
|
58
|
+
passwordhash TEXT NOT NULL,
|
|
59
|
+
salt TEXT NOT NULL,
|
|
60
|
+
verificationtoken TEXT,
|
|
61
|
+
verificationtokenexpires TIMESTAMP,
|
|
62
|
+
emailverified BOOLEAN DEFAULT FALSE,
|
|
63
|
+
resettoken TEXT,
|
|
64
|
+
resettokenexpires TIMESTAMP,
|
|
65
|
+
createdat TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
66
|
+
updatedat TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
67
|
+
)
|
|
68
|
+
`;
|
|
69
|
+
|
|
70
|
+
await db_query(client, createTableQuery);
|
|
71
|
+
|
|
72
|
+
// Create index on email for faster lookups
|
|
73
|
+
await db_query(client, 'CREATE INDEX IF NOT EXISTS idx_passwordauth_email ON passwordauth(email)');
|
|
74
|
+
await db_query(client, 'CREATE INDEX IF NOT EXISTS idx_passwordauth_verification_token ON passwordauth(verificationtoken)');
|
|
75
|
+
await db_query(client, 'CREATE INDEX IF NOT EXISTS idx_passwordauth_reset_token ON passwordauth(resettoken)');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get password auth record by email
|
|
80
|
+
*/
|
|
81
|
+
export async function getPasswordAuthByEmail(email: string): Promise<PasswordAuth | null> {
|
|
82
|
+
const client = await db_init();
|
|
83
|
+
|
|
84
|
+
const results = await db_query(client,
|
|
85
|
+
"SELECT * FROM passwordauth WHERE email = ?",
|
|
86
|
+
[email.toLowerCase()]
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return results.length > 0 ? results[0] as PasswordAuth : null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create a new password auth record (for signup)
|
|
94
|
+
*/
|
|
95
|
+
export async function createPasswordAuth(
|
|
96
|
+
email: string,
|
|
97
|
+
password: string
|
|
98
|
+
): Promise<{ passwordAuth: PasswordAuth; verificationToken: string }> {
|
|
99
|
+
const client = await db_init();
|
|
100
|
+
|
|
101
|
+
// Check if email already exists
|
|
102
|
+
const existing = await getPasswordAuthByEmail(email);
|
|
103
|
+
if (existing) {
|
|
104
|
+
throw new Error('Email already registered');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Hash the password
|
|
108
|
+
const { hash, salt } = await hashPassword(password);
|
|
109
|
+
|
|
110
|
+
// Generate verification token
|
|
111
|
+
const verificationToken = randomBytes(32).toString('base64url');
|
|
112
|
+
const verificationTokenExpires = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours
|
|
113
|
+
|
|
114
|
+
const id = crypto.randomUUID();
|
|
115
|
+
const now = new Date().toISOString();
|
|
116
|
+
|
|
117
|
+
await db_query(client,
|
|
118
|
+
`INSERT INTO passwordauth
|
|
119
|
+
(id, email, passwordhash, salt, verificationtoken, verificationtokenexpires, emailverified, createdat, updatedat)
|
|
120
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
121
|
+
[id, email.toLowerCase(), hash, salt, verificationToken, verificationTokenExpires.toISOString(), false, now, now]
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const passwordAuth = await getPasswordAuthByEmail(email);
|
|
125
|
+
if (!passwordAuth) {
|
|
126
|
+
throw new Error('Failed to create password auth record');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { passwordAuth, verificationToken };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Verify email with token
|
|
134
|
+
*/
|
|
135
|
+
export async function verifyEmailWithToken(token: string): Promise<PasswordAuth | null> {
|
|
136
|
+
const client = await db_init();
|
|
137
|
+
|
|
138
|
+
// Find the auth record with this token
|
|
139
|
+
const results = await db_query(client,
|
|
140
|
+
`SELECT * FROM passwordauth
|
|
141
|
+
WHERE verificationtoken = ?
|
|
142
|
+
AND verificationtokenexpires > CURRENT_TIMESTAMP
|
|
143
|
+
AND emailverified = false`,
|
|
144
|
+
[token]
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
if (results.length === 0) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const authRecord = results[0];
|
|
152
|
+
|
|
153
|
+
// Mark email as verified and clear token
|
|
154
|
+
await db_query(client,
|
|
155
|
+
`UPDATE passwordauth
|
|
156
|
+
SET emailverified = true,
|
|
157
|
+
verificationtoken = NULL,
|
|
158
|
+
verificationtokenexpires = NULL,
|
|
159
|
+
updatedat = CURRENT_TIMESTAMP
|
|
160
|
+
WHERE id = ?`,
|
|
161
|
+
[authRecord.id]
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
return await getPasswordAuthByEmail(authRecord.email);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Authenticate user with email and password
|
|
169
|
+
*/
|
|
170
|
+
export async function authenticateUser(email: string, password: string): Promise<PasswordAuth | null> {
|
|
171
|
+
const authRecord = await getPasswordAuthByEmail(email);
|
|
172
|
+
|
|
173
|
+
if (!authRecord) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Check if email is verified
|
|
178
|
+
if (!authRecord.emailverified) {
|
|
179
|
+
throw new Error('Email not verified');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Verify password
|
|
183
|
+
const isValid = await verifyPassword(password, authRecord.passwordhash, authRecord.salt);
|
|
184
|
+
|
|
185
|
+
if (!isValid) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return authRecord;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Generate password reset token
|
|
194
|
+
*/
|
|
195
|
+
export async function generatePasswordResetToken(email: string): Promise<string | null> {
|
|
196
|
+
const client = await db_init();
|
|
197
|
+
|
|
198
|
+
const authRecord = await getPasswordAuthByEmail(email);
|
|
199
|
+
if (!authRecord) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const resetToken = randomBytes(32).toString('base64url');
|
|
204
|
+
const resetTokenExpires = new Date(Date.now() + 60 * 60 * 1000); // 1 hour
|
|
205
|
+
|
|
206
|
+
await db_query(client,
|
|
207
|
+
`UPDATE passwordauth
|
|
208
|
+
SET resettoken = ?,
|
|
209
|
+
resettokenexpires = ?,
|
|
210
|
+
updatedat = CURRENT_TIMESTAMP
|
|
211
|
+
WHERE id = ?`,
|
|
212
|
+
[resetToken, resetTokenExpires.toISOString(), authRecord.id]
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
return resetToken;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Reset password with token
|
|
220
|
+
*/
|
|
221
|
+
export async function resetPasswordWithToken(token: string, newPassword: string): Promise<boolean> {
|
|
222
|
+
const client = await db_init();
|
|
223
|
+
|
|
224
|
+
// Find the auth record with this token
|
|
225
|
+
const results = await db_query(client,
|
|
226
|
+
`SELECT * FROM passwordauth
|
|
227
|
+
WHERE resettoken = ?
|
|
228
|
+
AND resettokenexpires > CURRENT_TIMESTAMP`,
|
|
229
|
+
[token]
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
if (results.length === 0) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const authRecord = results[0];
|
|
237
|
+
|
|
238
|
+
// Hash the new password
|
|
239
|
+
const { hash, salt } = await hashPassword(newPassword);
|
|
240
|
+
|
|
241
|
+
// Update password and clear reset token
|
|
242
|
+
await db_query(client,
|
|
243
|
+
`UPDATE passwordauth
|
|
244
|
+
SET passwordhash = ?,
|
|
245
|
+
salt = ?,
|
|
246
|
+
resettoken = NULL,
|
|
247
|
+
resettokenexpires = NULL,
|
|
248
|
+
updatedat = CURRENT_TIMESTAMP
|
|
249
|
+
WHERE id = ?`,
|
|
250
|
+
[hash, salt, authRecord.id]
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Delete unverified accounts older than 7 days
|
|
258
|
+
*/
|
|
259
|
+
export async function cleanupUnverifiedAccounts(): Promise<number> {
|
|
260
|
+
const client = await db_init();
|
|
261
|
+
|
|
262
|
+
const result = await db_query(client,
|
|
263
|
+
`DELETE FROM passwordauth
|
|
264
|
+
WHERE emailverified = false
|
|
265
|
+
AND createdat < datetime('now', '-7 days')`
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
return result[0]?.changes || 0;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Update password for authenticated user
|
|
273
|
+
*/
|
|
274
|
+
export async function updatePassword(email: string, currentPassword: string, newPassword: string): Promise<boolean> {
|
|
275
|
+
// First verify the current password
|
|
276
|
+
const authRecord = await authenticateUser(email, currentPassword);
|
|
277
|
+
if (!authRecord) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Hash the new password
|
|
282
|
+
const { hash, salt } = await hashPassword(newPassword);
|
|
283
|
+
const client = await db_init();
|
|
284
|
+
|
|
285
|
+
// Update password
|
|
286
|
+
await db_query(client,
|
|
287
|
+
`UPDATE passwordauth
|
|
288
|
+
SET passwordhash = ?,
|
|
289
|
+
salt = ?,
|
|
290
|
+
updatedat = CURRENT_TIMESTAMP
|
|
291
|
+
WHERE id = ?`,
|
|
292
|
+
[hash, salt, authRecord.id]
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Regenerate email verification token for unverified accounts
|
|
300
|
+
*/
|
|
301
|
+
export async function regenerateVerificationToken(email: string): Promise<{ passwordAuth: PasswordAuth; verificationToken: string } | null> {
|
|
302
|
+
const client = await db_init();
|
|
303
|
+
|
|
304
|
+
const authRecord = await getPasswordAuthByEmail(email);
|
|
305
|
+
if (!authRecord) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Only regenerate for unverified accounts
|
|
310
|
+
if (authRecord.emailverified) {
|
|
311
|
+
throw new Error('Email is already verified');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Generate new verification token
|
|
315
|
+
const verificationToken = randomBytes(32).toString('base64url');
|
|
316
|
+
const verificationTokenExpires = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours
|
|
317
|
+
|
|
318
|
+
await db_query(client,
|
|
319
|
+
`UPDATE passwordauth
|
|
320
|
+
SET verificationtoken = ?,
|
|
321
|
+
verificationtokenexpires = ?,
|
|
322
|
+
updatedat = CURRENT_TIMESTAMP
|
|
323
|
+
WHERE id = ?`,
|
|
324
|
+
[verificationToken, verificationTokenExpires.toISOString(), authRecord.id]
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
const updatedAuth = await getPasswordAuthByEmail(email);
|
|
328
|
+
if (!updatedAuth) {
|
|
329
|
+
throw new Error('Failed to update verification token');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return { passwordAuth: updatedAuth, verificationToken };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Check if an email is already registered
|
|
337
|
+
*/
|
|
338
|
+
export async function isEmailRegistered(email: string): Promise<boolean> {
|
|
339
|
+
const authRecord = await getPasswordAuthByEmail(email);
|
|
340
|
+
return authRecord !== null;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Get statistics about password auth users
|
|
345
|
+
*/
|
|
346
|
+
export async function getPasswordAuthStats(): Promise<{
|
|
347
|
+
totalUsers: number;
|
|
348
|
+
verifiedUsers: number;
|
|
349
|
+
unverifiedUsers: number;
|
|
350
|
+
}> {
|
|
351
|
+
const client = await db_init();
|
|
352
|
+
|
|
353
|
+
const stats = await db_query(client, `
|
|
354
|
+
SELECT
|
|
355
|
+
COUNT(*) as totalUsers,
|
|
356
|
+
SUM(CASE WHEN emailverified = true THEN 1 ELSE 0 END) as verifiedUsers,
|
|
357
|
+
SUM(CASE WHEN emailverified = false THEN 1 ELSE 0 END) as unverifiedUsers
|
|
358
|
+
FROM passwordauth
|
|
359
|
+
`);
|
|
360
|
+
|
|
361
|
+
return stats[0];
|
|
362
|
+
}
|
|
@@ -10,7 +10,7 @@ import * as crypto from 'node:crypto';
|
|
|
10
10
|
export interface User {
|
|
11
11
|
userid: string;
|
|
12
12
|
OAuthID: string;
|
|
13
|
-
Source: 'google' | 'facebook' | 'apple' | 'github';
|
|
13
|
+
Source: 'google' | 'facebook' | 'apple' | 'github' | 'userpass';
|
|
14
14
|
UserName: string;
|
|
15
15
|
Email?: string;
|
|
16
16
|
AvatarURL?: string;
|
|
@@ -48,7 +48,7 @@ export async function getAllUsers(): Promise<User[]> {
|
|
|
48
48
|
`SELECT * FROM users
|
|
49
49
|
WHERE IsActive = ?
|
|
50
50
|
ORDER BY UserLevel DESC, UserName ASC`,
|
|
51
|
-
[true
|
|
51
|
+
[1] // Use 1 instead of true for PostgreSQL compatibility
|
|
52
52
|
);
|
|
53
53
|
|
|
54
54
|
return users as User[];
|
|
@@ -62,7 +62,7 @@ export async function getUserByOAuthID(oauthId: string): Promise<User | null> {
|
|
|
62
62
|
|
|
63
63
|
const users = await db_query(client,
|
|
64
64
|
"SELECT * FROM users WHERE OAuthID = ? AND IsActive = ?",
|
|
65
|
-
[oauthId, true
|
|
65
|
+
[oauthId, 1] // Use 1 instead of true for PostgreSQL compatibility
|
|
66
66
|
);
|
|
67
67
|
|
|
68
68
|
return users.length > 0 ? users[0] as User : null;
|
|
@@ -76,7 +76,7 @@ export async function getUserByID(userId: string): Promise<User | null> {
|
|
|
76
76
|
|
|
77
77
|
const users = await db_query(client,
|
|
78
78
|
"SELECT * FROM users WHERE userid = ? AND IsActive = ?",
|
|
79
|
-
[userId, true
|
|
79
|
+
[userId, 1] // Use 1 instead of true for PostgreSQL compatibility
|
|
80
80
|
);
|
|
81
81
|
|
|
82
82
|
return users.length > 0 ? users[0] as User : null;
|
|
@@ -87,7 +87,7 @@ export async function getUserByID(userId: string): Promise<User | null> {
|
|
|
87
87
|
*/
|
|
88
88
|
export async function upsertUser(
|
|
89
89
|
oauthId: string,
|
|
90
|
-
source: 'google' | 'facebook' | 'apple' | 'github',
|
|
90
|
+
source: 'google' | 'facebook' | 'apple' | 'github' | 'userpass',
|
|
91
91
|
userName: string,
|
|
92
92
|
email?: string,
|
|
93
93
|
avatarUrl?: string
|
|
@@ -122,7 +122,7 @@ export async function upsertUser(
|
|
|
122
122
|
(userid, OAuthID, Source, UserName, Email, AvatarURL, UserLevel, UserTier,
|
|
123
123
|
LastLoginDate, CreatedDate, IsActive)
|
|
124
124
|
VALUES (?, ?, ?, ?, ?, ?, ?, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)`,
|
|
125
|
-
[userId, oauthId, source, userName, email || null, avatarUrl || '', permissionLevel, true
|
|
125
|
+
[userId, oauthId, source, userName, email || null, avatarUrl || '', permissionLevel, 1] // Use 1 instead of true for PostgreSQL compatibility
|
|
126
126
|
);
|
|
127
127
|
|
|
128
128
|
return (await getUserByOAuthID(oauthId))!;
|
|
@@ -142,7 +142,7 @@ export async function updateUserPermission(
|
|
|
142
142
|
if (permissionLevel < 2) {
|
|
143
143
|
const admins = await db_query(client,
|
|
144
144
|
"SELECT COUNT(*) as count FROM users WHERE UserLevel = ? AND userid != ? AND IsActive = ?",
|
|
145
|
-
[2, userId, true
|
|
145
|
+
[2, userId, 1] // Use 1 instead of true for PostgreSQL compatibility
|
|
146
146
|
);
|
|
147
147
|
|
|
148
148
|
if (admins[0].count === 0) {
|
|
@@ -169,7 +169,7 @@ export async function deleteUser(userId: string): Promise<boolean> {
|
|
|
169
169
|
if (user && user.UserLevel === 2) {
|
|
170
170
|
const admins = await db_query(client,
|
|
171
171
|
"SELECT COUNT(*) as count FROM users WHERE UserLevel = ? AND userid != ? AND IsActive = ?",
|
|
172
|
-
[2, userId, true
|
|
172
|
+
[2, userId, 1] // Use 1 instead of true for PostgreSQL compatibility
|
|
173
173
|
);
|
|
174
174
|
|
|
175
175
|
if (admins[0].count === 0) {
|
|
@@ -180,7 +180,7 @@ export async function deleteUser(userId: string): Promise<boolean> {
|
|
|
180
180
|
// Soft delete the user
|
|
181
181
|
const result = await db_query(client,
|
|
182
182
|
"UPDATE users SET IsActive = ? WHERE userid = ?",
|
|
183
|
-
[
|
|
183
|
+
[0, userId] // Use 0 instead of false for PostgreSQL compatibility
|
|
184
184
|
);
|
|
185
185
|
|
|
186
186
|
return result[0].changes > 0;
|
|
@@ -205,7 +205,7 @@ export async function getUserStats(): Promise<{
|
|
|
205
205
|
SUM(CASE WHEN UserLevel = 0 THEN 1 ELSE 0 END) as guestUsers
|
|
206
206
|
FROM users
|
|
207
207
|
WHERE IsActive = ?
|
|
208
|
-
`, [
|
|
208
|
+
`, [1]); // Use 1 instead of true for PostgreSQL compatibility
|
|
209
209
|
|
|
210
210
|
return stats[0];
|
|
211
211
|
}
|
|
@@ -286,7 +286,7 @@ export async function validateSession(sessionToken: string): Promise<{
|
|
|
286
286
|
`SELECT s.*, u.* FROM usersession s
|
|
287
287
|
JOIN users u ON s.userid = u.userid
|
|
288
288
|
WHERE s.SessionToken = ? AND s.ExpirationDate > CURRENT_TIMESTAMP AND u.IsActive = ?`,
|
|
289
|
-
[sessionToken, true
|
|
289
|
+
[sessionToken, 1] // Use 1 instead of true for PostgreSQL compatibility
|
|
290
290
|
);
|
|
291
291
|
|
|
292
292
|
if (!sessions || sessions.length === 0) {
|
|
@@ -89,23 +89,21 @@ export function middleware(request: NextRequest) {
|
|
|
89
89
|
// Using Report-Only mode to monitor CSP violations without breaking functionality
|
|
90
90
|
response.headers.set('Content-Security-Policy-Report-Only', csp)
|
|
91
91
|
|
|
92
|
-
// Add CSRF token generation for
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
});
|
|
108
|
-
}
|
|
92
|
+
// Add CSRF token generation for all requests
|
|
93
|
+
const csrfToken = request.cookies.get('csrf-token')?.value;
|
|
94
|
+
|
|
95
|
+
// If no CSRF token exists, generate one
|
|
96
|
+
if (!csrfToken) {
|
|
97
|
+
const newCsrfToken = crypto.randomUUID();
|
|
98
|
+
response.cookies.set({
|
|
99
|
+
name: 'csrf-token',
|
|
100
|
+
value: newCsrfToken,
|
|
101
|
+
httpOnly: true,
|
|
102
|
+
secure: process.env.NODE_ENV === 'production',
|
|
103
|
+
sameSite: 'strict',
|
|
104
|
+
maxAge: 60 * 60 * 24, // 24 hours
|
|
105
|
+
path: '/'
|
|
106
|
+
});
|
|
109
107
|
}
|
|
110
108
|
|
|
111
109
|
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { randomBytes } from 'crypto';
|
|
2
|
+
|
|
3
|
+
// Simple in-memory storage for email tracking (in production, use a database)
|
|
4
|
+
const emailHistory: Map<string, any> = new Map();
|
|
5
|
+
|
|
6
|
+
export interface EmailOptions {
|
|
7
|
+
to: string | string[];
|
|
8
|
+
from: string;
|
|
9
|
+
subject: string;
|
|
10
|
+
html?: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
cc?: string | string[];
|
|
13
|
+
bcc?: string | string[];
|
|
14
|
+
replyTo?: string | string[];
|
|
15
|
+
attachments?: Array<{
|
|
16
|
+
filename: string;
|
|
17
|
+
content: string; // base64 encoded content
|
|
18
|
+
contentType?: string;
|
|
19
|
+
}>;
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
tags?: Array<{
|
|
22
|
+
name: string;
|
|
23
|
+
value: string;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Send email function that sends emails via the Resend API
|
|
29
|
+
* @param options Email options including to, from, subject, message content, etc.
|
|
30
|
+
* @returns Promise that resolves to the email ID from Resend
|
|
31
|
+
*/
|
|
32
|
+
export async function send_email(options: EmailOptions): Promise<string> {
|
|
33
|
+
console.log(`📧 Sending email to: ${Array.isArray(options.to) ? options.to.join(', ') : options.to}`);
|
|
34
|
+
console.log(`📧 Subject: ${options.subject}`);
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
// Get API key from environment
|
|
38
|
+
const apiKey = process.env.RESEND_API_KEY;
|
|
39
|
+
if (!apiKey) {
|
|
40
|
+
throw new Error('RESEND_API_KEY environment variable is not set');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Prepare request body for Resend API
|
|
44
|
+
const requestBody: any = {
|
|
45
|
+
to: options.to,
|
|
46
|
+
from: options.from,
|
|
47
|
+
subject: options.subject,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Add optional fields
|
|
51
|
+
if (options.html) requestBody.html = options.html;
|
|
52
|
+
if (options.text) requestBody.text = options.text;
|
|
53
|
+
if (options.cc) requestBody.cc = options.cc;
|
|
54
|
+
if (options.bcc) requestBody.bcc = options.bcc;
|
|
55
|
+
if (options.replyTo) requestBody.reply_to = options.replyTo;
|
|
56
|
+
if (options.headers) requestBody.headers = options.headers;
|
|
57
|
+
if (options.tags) requestBody.tags = options.tags;
|
|
58
|
+
|
|
59
|
+
// Handle attachments
|
|
60
|
+
if (options.attachments && options.attachments.length > 0) {
|
|
61
|
+
requestBody.attachments = options.attachments.map(att => ({
|
|
62
|
+
filename: att.filename,
|
|
63
|
+
content: att.content,
|
|
64
|
+
content_type: att.contentType,
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Make API request to Resend
|
|
69
|
+
const response = await fetch('https://api.resend.com/emails', {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: {
|
|
72
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
73
|
+
'Content-Type': 'application/json',
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify(requestBody),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
const errorData = await response.json().catch(() => ({ message: 'Unknown error' }));
|
|
80
|
+
throw new Error(`Resend API error: ${response.status} - ${errorData.message || response.statusText}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const result = await response.json();
|
|
84
|
+
const emailId = result.id;
|
|
85
|
+
|
|
86
|
+
// Store email metadata
|
|
87
|
+
const emailKey = `${Date.now()}_${randomBytes(6).toString('hex')}`;
|
|
88
|
+
emailHistory.set(emailKey, {
|
|
89
|
+
id: emailId,
|
|
90
|
+
to: options.to,
|
|
91
|
+
from: options.from,
|
|
92
|
+
subject: options.subject,
|
|
93
|
+
sentAt: new Date().toISOString(),
|
|
94
|
+
status: 'sent',
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
console.log(`✅ Email sent successfully. ID: ${emailId}`);
|
|
98
|
+
|
|
99
|
+
return emailId;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error('❌ Error sending email:', error);
|
|
102
|
+
throw new Error(`Failed to send email: ${error}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get email status by ID
|
|
108
|
+
* @param emailId The email ID returned from send_email
|
|
109
|
+
* @returns Email metadata or undefined if not found
|
|
110
|
+
*/
|
|
111
|
+
export async function getEmailStatus(emailId: string): Promise<any> {
|
|
112
|
+
// Find email in history by Resend ID
|
|
113
|
+
for (const [key, data] of emailHistory.entries()) {
|
|
114
|
+
if (data.id === emailId) {
|
|
115
|
+
return data;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If not found locally, could query Resend API for status
|
|
120
|
+
const apiKey = process.env.RESEND_API_KEY;
|
|
121
|
+
if (!apiKey) {
|
|
122
|
+
throw new Error('RESEND_API_KEY environment variable is not set');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const response = await fetch(`https://api.resend.com/emails/${emailId}`, {
|
|
127
|
+
headers: {
|
|
128
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return await response.json();
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error('Error fetching email status:', error);
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* List sent emails from local history
|
|
145
|
+
* @returns Array of email metadata
|
|
146
|
+
*/
|
|
147
|
+
export function listSentEmails(): Array<any> {
|
|
148
|
+
return Array.from(emailHistory.values());
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Initialize global send_email function for generated API code
|
|
153
|
+
* This makes send_email available in the same way as the preview environment
|
|
154
|
+
*/
|
|
155
|
+
export function initializeGlobalEmailAPI(): void {
|
|
156
|
+
// Make send_email available globally for API code execution
|
|
157
|
+
(global as any).send_email = send_email;
|
|
158
|
+
(global as any).getEmailStatus = getEmailStatus;
|
|
159
|
+
(global as any).listSentEmails = listSentEmails;
|
|
160
|
+
|
|
161
|
+
console.log('✅ Global email API initialized');
|
|
162
|
+
}
|
package/dist/generators/apis.js
CHANGED
|
@@ -245,6 +245,7 @@ This file is generated by Sitepaige.
|
|
|
245
245
|
import { NextRequest, NextResponse } from 'next/server';
|
|
246
246
|
import { check_auth } from '${relativePathToRoot}auth/auth';
|
|
247
247
|
import { store_file } from '${relativePathToRoot}storage/files';
|
|
248
|
+
import { send_email } from '${relativePathToRoot}storage/email';
|
|
248
249
|
import { db_init, db_query } from '${relativePathToRoot}db';
|
|
249
250
|
`;
|
|
250
251
|
const routeCode = header + "\n" + processedApiCode + "\n";
|