@payez/next-mvp 4.0.0 → 4.0.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/dist/api-handlers/account/change-password.js +110 -110
- package/dist/api-handlers/admin/analytics.d.ts +19 -19
- package/dist/api-handlers/admin/analytics.js +378 -378
- package/dist/api-handlers/admin/audit.d.ts +19 -19
- package/dist/api-handlers/admin/audit.js +213 -213
- package/dist/api-handlers/admin/index.d.ts +21 -21
- package/dist/api-handlers/admin/index.js +42 -42
- package/dist/api-handlers/admin/redis-sessions.d.ts +35 -35
- package/dist/api-handlers/admin/redis-sessions.js +203 -203
- package/dist/api-handlers/admin/sessions.d.ts +20 -20
- package/dist/api-handlers/admin/sessions.js +283 -283
- package/dist/api-handlers/admin/site-logs.d.ts +45 -45
- package/dist/api-handlers/admin/site-logs.js +317 -317
- package/dist/api-handlers/admin/stats.d.ts +20 -20
- package/dist/api-handlers/admin/stats.js +239 -239
- package/dist/api-handlers/admin/users.d.ts +19 -19
- package/dist/api-handlers/admin/users.js +221 -221
- package/dist/api-handlers/admin/vibe-data.d.ts +79 -79
- package/dist/api-handlers/admin/vibe-data.js +267 -267
- package/dist/api-handlers/auth/refresh.js +633 -633
- package/dist/api-handlers/auth/signout.js +186 -186
- package/dist/api-handlers/auth/verify-code.d.ts +43 -43
- package/dist/api-handlers/auth/verify-code.js +90 -90
- package/dist/api-handlers/session/viability.js +114 -114
- package/dist/api-handlers/test/force-expire.js +59 -59
- package/dist/auth/auth-decision.js +182 -182
- package/dist/auth/utils/token-utils.d.ts +83 -83
- package/dist/auth/utils/token-utils.js +218 -218
- package/dist/client/AuthContext.js +115 -115
- package/dist/client/better-auth-client.d.ts +1020 -1020
- package/dist/components/SessionSync.js +121 -121
- package/dist/components/account/MobileNavDrawer.js +64 -64
- package/dist/components/account/UserAvatarMenu.js +91 -91
- package/dist/components/admin/VibeAdminLayout.js +71 -71
- package/dist/hooks/useAuthSettings.js +93 -93
- package/dist/hooks/useAvailableProviders.d.ts +43 -43
- package/dist/hooks/useAvailableProviders.js +112 -112
- package/dist/lib/app-slug.d.ts +95 -95
- package/dist/lib/app-slug.js +172 -172
- package/dist/lib/test-aware-get-token.js +86 -86
- package/dist/lib/token-lifecycle.d.ts +78 -78
- package/dist/lib/token-lifecycle.js +360 -360
- package/dist/pages/admin-login/page.js +73 -73
- package/dist/pages/client-admin/ClientSiteAdminPage.js +179 -179
- package/dist/pages/login/page.js +202 -202
- package/dist/pages/showcase/ShowcasePage.js +142 -142
- package/dist/pages/test-env/EmergencyLogoutPage.js +99 -99
- package/dist/pages/test-env/JwtInspectPage.js +116 -116
- package/dist/pages/test-env/TestEnvPage.js +51 -51
- package/dist/pages/verify-code/page.js +412 -412
- package/dist/routes/auth/logout.d.ts +31 -31
- package/dist/routes/auth/logout.js +98 -98
- package/dist/routes/auth/session.js +157 -157
- package/dist/routes/auth/viability.js +190 -190
- package/package.json +6 -16
- package/dist/auth/auth-options.d.ts +0 -57
- package/dist/auth/auth-options.js +0 -213
- package/dist/auth/callbacks/index.d.ts +0 -6
- package/dist/auth/callbacks/index.js +0 -12
- package/dist/auth/callbacks/jwt.d.ts +0 -45
- package/dist/auth/callbacks/jwt.js +0 -305
- package/dist/auth/callbacks/session.d.ts +0 -60
- package/dist/auth/callbacks/session.js +0 -170
- package/dist/auth/callbacks/signin.d.ts +0 -23
- package/dist/auth/callbacks/signin.js +0 -44
- package/dist/auth/events/index.d.ts +0 -4
- package/dist/auth/events/index.js +0 -8
- package/dist/auth/events/signout.d.ts +0 -17
- package/dist/auth/events/signout.js +0 -32
- package/dist/auth/providers/credentials.d.ts +0 -32
- package/dist/auth/providers/credentials.js +0 -223
- package/dist/auth/providers/index.d.ts +0 -5
- package/dist/auth/providers/index.js +0 -21
- package/dist/auth/providers/oauth.d.ts +0 -26
- package/dist/auth/providers/oauth.js +0 -105
- package/dist/lib/nextauth-secret.d.ts +0 -10
- package/dist/lib/nextauth-secret.js +0 -100
- package/dist/pages/profile/profile-patch.d.ts +0 -1
- package/dist/pages/profile/profile-patch.js +0 -281
- package/dist/pages/security/security-patch.d.ts +0 -1
- package/dist/pages/security/security-patch.js +0 -302
|
@@ -1,221 +1,221 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Admin Users API Handler
|
|
4
|
-
*
|
|
5
|
-
* Provides admin-level access to users using service account credentials.
|
|
6
|
-
*
|
|
7
|
-
* @version 1.0
|
|
8
|
-
* @requires Admin role (vibe_app_admin or payez_admin)
|
|
9
|
-
*/
|
|
10
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
14
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
15
|
-
}
|
|
16
|
-
Object.defineProperty(o, k2, desc);
|
|
17
|
-
}) : (function(o, m, k, k2) {
|
|
18
|
-
if (k2 === undefined) k2 = k;
|
|
19
|
-
o[k2] = m[k];
|
|
20
|
-
}));
|
|
21
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
22
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
23
|
-
}) : function(o, v) {
|
|
24
|
-
o["default"] = v;
|
|
25
|
-
});
|
|
26
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
27
|
-
var ownKeys = function(o) {
|
|
28
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
29
|
-
var ar = [];
|
|
30
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
|
-
return ownKeys(o);
|
|
34
|
-
};
|
|
35
|
-
return function (mod) {
|
|
36
|
-
if (mod && mod.__esModule) return mod;
|
|
37
|
-
var result = {};
|
|
38
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
39
|
-
__setModuleDefault(result, mod);
|
|
40
|
-
return result;
|
|
41
|
-
};
|
|
42
|
-
})();
|
|
43
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.createUsersHandler = createUsersHandler;
|
|
45
|
-
const server_1 = require("next/server");
|
|
46
|
-
const auth_1 = require("../../server/auth");
|
|
47
|
-
const startup_init_1 = require("../../lib/startup-init");
|
|
48
|
-
const roles_1 = require("../../lib/roles");
|
|
49
|
-
async function checkAdminRole(request) {
|
|
50
|
-
const session = await (0, auth_1.getSession)(request);
|
|
51
|
-
if (!session?.user) {
|
|
52
|
-
return {
|
|
53
|
-
isAdmin: false,
|
|
54
|
-
error: server_1.NextResponse.json({ success: false, error: 'Please sign in' }, { status: 401 }),
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
const userRoles = session.user?.roles || [];
|
|
58
|
-
const hasAdminRole = roles_1.ADMIN_ROLES.some(role => userRoles.includes(role));
|
|
59
|
-
if (!hasAdminRole) {
|
|
60
|
-
return {
|
|
61
|
-
isAdmin: false,
|
|
62
|
-
error: server_1.NextResponse.json({ success: false, error: 'Admin access required' }, { status: 403 }),
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
return { isAdmin: true };
|
|
66
|
-
}
|
|
67
|
-
async function vibeServiceRequest(endpoint, options) {
|
|
68
|
-
const idpUrl = process.env.NEXT_PUBLIC_IDP_URL || process.env.IDP_URL;
|
|
69
|
-
const clientId = process.env.VIBE_CLIENT_ID;
|
|
70
|
-
const signingKey = process.env.VIBE_HMAC_KEY;
|
|
71
|
-
if (!idpUrl || !clientId || !signingKey) {
|
|
72
|
-
return { ok: false, status: 500, data: null, error: 'Vibe not configured' };
|
|
73
|
-
}
|
|
74
|
-
const timestamp = Math.floor(Date.now() / 1000);
|
|
75
|
-
const stringToSign = `${timestamp}|${options.method}|${endpoint}`;
|
|
76
|
-
const crypto = await Promise.resolve().then(() => __importStar(require('crypto')));
|
|
77
|
-
const signature = crypto
|
|
78
|
-
.createHmac('sha256', Buffer.from(signingKey, 'base64'))
|
|
79
|
-
.update(stringToSign)
|
|
80
|
-
.digest('base64');
|
|
81
|
-
const proxyUrl = `${idpUrl}/api/vibe/proxy`;
|
|
82
|
-
// Get the client slug from startup config for multi-client admin support
|
|
83
|
-
const idpConfig = (0, startup_init_1.getStartupIDPConfig)();
|
|
84
|
-
const idpClientId = idpConfig?.clientSlug || idpConfig?.clientId;
|
|
85
|
-
try {
|
|
86
|
-
const res = await fetch(proxyUrl, {
|
|
87
|
-
method: 'POST',
|
|
88
|
-
headers: {
|
|
89
|
-
'Content-Type': 'application/json',
|
|
90
|
-
'X-Vibe-Client-Id': clientId,
|
|
91
|
-
'X-Vibe-Timestamp': String(timestamp),
|
|
92
|
-
'X-Vibe-Signature': signature,
|
|
93
|
-
...(idpClientId && { 'X-Client-Id': idpClientId }),
|
|
94
|
-
},
|
|
95
|
-
body: JSON.stringify({
|
|
96
|
-
endpoint,
|
|
97
|
-
method: options.method,
|
|
98
|
-
data: options.body ?? null,
|
|
99
|
-
}),
|
|
100
|
-
cache: 'no-store',
|
|
101
|
-
});
|
|
102
|
-
if (res.status === 204)
|
|
103
|
-
return { ok: true, status: 204, data: null };
|
|
104
|
-
if (!res.ok) {
|
|
105
|
-
const errorText = await res.text();
|
|
106
|
-
return { ok: false, status: res.status, data: null, error: errorText };
|
|
107
|
-
}
|
|
108
|
-
const body = await res.json();
|
|
109
|
-
return { ok: true, status: res.status, data: body };
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
return { ok: false, status: 0, data: null, error: String(error) };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* GET /api/admin/users - List users
|
|
117
|
-
* POST /api/admin/users - Stats, search, update tier
|
|
118
|
-
*/
|
|
119
|
-
function createUsersHandler(config) {
|
|
120
|
-
return {
|
|
121
|
-
async GET(request) {
|
|
122
|
-
const adminCheck = await checkAdminRole(request);
|
|
123
|
-
if (adminCheck.error)
|
|
124
|
-
return adminCheck.error;
|
|
125
|
-
const { searchParams } = new URL(request.url);
|
|
126
|
-
const search = searchParams.get('search');
|
|
127
|
-
const tier = searchParams.get('tier');
|
|
128
|
-
const page = parseInt(searchParams.get('page') || '1');
|
|
129
|
-
const pageSize = parseInt(searchParams.get('pageSize') || '50');
|
|
130
|
-
const queryBody = {
|
|
131
|
-
page,
|
|
132
|
-
pageSize,
|
|
133
|
-
orderBy: 'created_at',
|
|
134
|
-
orderDirection: 'desc',
|
|
135
|
-
};
|
|
136
|
-
const conditions = [];
|
|
137
|
-
if (search) {
|
|
138
|
-
conditions.push({
|
|
139
|
-
operator: 'or',
|
|
140
|
-
conditions: [
|
|
141
|
-
{ field: 'email', operator: 'like', value: `%${search}%` },
|
|
142
|
-
{ field: 'display_name', operator: 'like', value: `%${search}%` },
|
|
143
|
-
],
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
if (tier) {
|
|
147
|
-
conditions.push({ field: 'tier', operator: 'eq', value: tier });
|
|
148
|
-
}
|
|
149
|
-
if (conditions.length === 1) {
|
|
150
|
-
queryBody.filter = conditions[0];
|
|
151
|
-
}
|
|
152
|
-
else if (conditions.length > 1) {
|
|
153
|
-
queryBody.filter = { operator: 'and', conditions };
|
|
154
|
-
}
|
|
155
|
-
const result = await vibeServiceRequest('/v1/collections/vibe_app/tables/users/query', { method: 'POST', body: queryBody });
|
|
156
|
-
if (!result.ok) {
|
|
157
|
-
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
158
|
-
}
|
|
159
|
-
const rawUsers = result.data?.data || result.data?.documents || [];
|
|
160
|
-
const users = rawUsers.map((u) => ({
|
|
161
|
-
id: u.id || u.document_id,
|
|
162
|
-
idp_user_id: u.idp_user_id,
|
|
163
|
-
email: u.email,
|
|
164
|
-
display_name: u.display_name || u.name,
|
|
165
|
-
tier: u.tier || 'free',
|
|
166
|
-
credits: u.credits || 0,
|
|
167
|
-
status: u.status || 'active',
|
|
168
|
-
created_at: u.created_at,
|
|
169
|
-
last_login: u.last_login || u.updated_at,
|
|
170
|
-
}));
|
|
171
|
-
return server_1.NextResponse.json({
|
|
172
|
-
users,
|
|
173
|
-
meta: result.data?.meta || { total: users.length, page, pageSize },
|
|
174
|
-
});
|
|
175
|
-
},
|
|
176
|
-
async POST(request) {
|
|
177
|
-
const adminCheck = await checkAdminRole(request);
|
|
178
|
-
if (adminCheck.error)
|
|
179
|
-
return adminCheck.error;
|
|
180
|
-
const body = await request.json();
|
|
181
|
-
const { action, userId, tier, credits } = body;
|
|
182
|
-
if (action === 'stats') {
|
|
183
|
-
const result = await vibeServiceRequest('/v1/collections/vibe_app/tables/users/query', { method: 'POST', body: { page: 1, pageSize: 10000 } });
|
|
184
|
-
if (!result.ok) {
|
|
185
|
-
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
186
|
-
}
|
|
187
|
-
const users = result.data?.data || result.data?.documents || [];
|
|
188
|
-
const now = new Date();
|
|
189
|
-
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
190
|
-
const oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
|
191
|
-
const stats = {
|
|
192
|
-
total: users.length,
|
|
193
|
-
active: users.filter((u) => u.status === 'active').length,
|
|
194
|
-
newToday: users.filter((u) => new Date(u.created_at) > oneDayAgo).length,
|
|
195
|
-
newThisWeek: users.filter((u) => new Date(u.created_at) > oneWeekAgo).length,
|
|
196
|
-
byTier: {},
|
|
197
|
-
};
|
|
198
|
-
users.forEach((u) => {
|
|
199
|
-
const t = u.tier || 'free';
|
|
200
|
-
stats.byTier[t] = (stats.byTier[t] || 0) + 1;
|
|
201
|
-
});
|
|
202
|
-
return server_1.NextResponse.json({ stats });
|
|
203
|
-
}
|
|
204
|
-
if (action === 'update_tier' && userId && tier) {
|
|
205
|
-
const result = await vibeServiceRequest(`/v1/collections/vibe_app/tables/users/${userId}`, { method: 'PUT', body: { tier } });
|
|
206
|
-
if (!result.ok) {
|
|
207
|
-
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
208
|
-
}
|
|
209
|
-
return server_1.NextResponse.json({ success: true });
|
|
210
|
-
}
|
|
211
|
-
if (action === 'update_credits' && userId && credits !== undefined) {
|
|
212
|
-
const result = await vibeServiceRequest(`/v1/collections/vibe_app/tables/users/${userId}`, { method: 'PUT', body: { credits } });
|
|
213
|
-
if (!result.ok) {
|
|
214
|
-
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
215
|
-
}
|
|
216
|
-
return server_1.NextResponse.json({ success: true });
|
|
217
|
-
}
|
|
218
|
-
return server_1.NextResponse.json({ error: 'Invalid action' }, { status: 400 });
|
|
219
|
-
},
|
|
220
|
-
};
|
|
221
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Admin Users API Handler
|
|
4
|
+
*
|
|
5
|
+
* Provides admin-level access to users using service account credentials.
|
|
6
|
+
*
|
|
7
|
+
* @version 1.0
|
|
8
|
+
* @requires Admin role (vibe_app_admin or payez_admin)
|
|
9
|
+
*/
|
|
10
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
14
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
17
|
+
}) : (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
o[k2] = m[k];
|
|
20
|
+
}));
|
|
21
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
22
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
23
|
+
}) : function(o, v) {
|
|
24
|
+
o["default"] = v;
|
|
25
|
+
});
|
|
26
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
27
|
+
var ownKeys = function(o) {
|
|
28
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
29
|
+
var ar = [];
|
|
30
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
return ownKeys(o);
|
|
34
|
+
};
|
|
35
|
+
return function (mod) {
|
|
36
|
+
if (mod && mod.__esModule) return mod;
|
|
37
|
+
var result = {};
|
|
38
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
39
|
+
__setModuleDefault(result, mod);
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
})();
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.createUsersHandler = createUsersHandler;
|
|
45
|
+
const server_1 = require("next/server");
|
|
46
|
+
const auth_1 = require("../../server/auth");
|
|
47
|
+
const startup_init_1 = require("../../lib/startup-init");
|
|
48
|
+
const roles_1 = require("../../lib/roles");
|
|
49
|
+
async function checkAdminRole(request) {
|
|
50
|
+
const session = await (0, auth_1.getSession)(request);
|
|
51
|
+
if (!session?.user) {
|
|
52
|
+
return {
|
|
53
|
+
isAdmin: false,
|
|
54
|
+
error: server_1.NextResponse.json({ success: false, error: 'Please sign in' }, { status: 401 }),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const userRoles = session.user?.roles || [];
|
|
58
|
+
const hasAdminRole = roles_1.ADMIN_ROLES.some(role => userRoles.includes(role));
|
|
59
|
+
if (!hasAdminRole) {
|
|
60
|
+
return {
|
|
61
|
+
isAdmin: false,
|
|
62
|
+
error: server_1.NextResponse.json({ success: false, error: 'Admin access required' }, { status: 403 }),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return { isAdmin: true };
|
|
66
|
+
}
|
|
67
|
+
async function vibeServiceRequest(endpoint, options) {
|
|
68
|
+
const idpUrl = process.env.NEXT_PUBLIC_IDP_URL || process.env.IDP_URL;
|
|
69
|
+
const clientId = process.env.VIBE_CLIENT_ID;
|
|
70
|
+
const signingKey = process.env.VIBE_HMAC_KEY;
|
|
71
|
+
if (!idpUrl || !clientId || !signingKey) {
|
|
72
|
+
return { ok: false, status: 500, data: null, error: 'Vibe not configured' };
|
|
73
|
+
}
|
|
74
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
75
|
+
const stringToSign = `${timestamp}|${options.method}|${endpoint}`;
|
|
76
|
+
const crypto = await Promise.resolve().then(() => __importStar(require('crypto')));
|
|
77
|
+
const signature = crypto
|
|
78
|
+
.createHmac('sha256', Buffer.from(signingKey, 'base64'))
|
|
79
|
+
.update(stringToSign)
|
|
80
|
+
.digest('base64');
|
|
81
|
+
const proxyUrl = `${idpUrl}/api/vibe/proxy`;
|
|
82
|
+
// Get the client slug from startup config for multi-client admin support
|
|
83
|
+
const idpConfig = (0, startup_init_1.getStartupIDPConfig)();
|
|
84
|
+
const idpClientId = idpConfig?.clientSlug || idpConfig?.clientId;
|
|
85
|
+
try {
|
|
86
|
+
const res = await fetch(proxyUrl, {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: {
|
|
89
|
+
'Content-Type': 'application/json',
|
|
90
|
+
'X-Vibe-Client-Id': clientId,
|
|
91
|
+
'X-Vibe-Timestamp': String(timestamp),
|
|
92
|
+
'X-Vibe-Signature': signature,
|
|
93
|
+
...(idpClientId && { 'X-Client-Id': idpClientId }),
|
|
94
|
+
},
|
|
95
|
+
body: JSON.stringify({
|
|
96
|
+
endpoint,
|
|
97
|
+
method: options.method,
|
|
98
|
+
data: options.body ?? null,
|
|
99
|
+
}),
|
|
100
|
+
cache: 'no-store',
|
|
101
|
+
});
|
|
102
|
+
if (res.status === 204)
|
|
103
|
+
return { ok: true, status: 204, data: null };
|
|
104
|
+
if (!res.ok) {
|
|
105
|
+
const errorText = await res.text();
|
|
106
|
+
return { ok: false, status: res.status, data: null, error: errorText };
|
|
107
|
+
}
|
|
108
|
+
const body = await res.json();
|
|
109
|
+
return { ok: true, status: res.status, data: body };
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
return { ok: false, status: 0, data: null, error: String(error) };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* GET /api/admin/users - List users
|
|
117
|
+
* POST /api/admin/users - Stats, search, update tier
|
|
118
|
+
*/
|
|
119
|
+
function createUsersHandler(config) {
|
|
120
|
+
return {
|
|
121
|
+
async GET(request) {
|
|
122
|
+
const adminCheck = await checkAdminRole(request);
|
|
123
|
+
if (adminCheck.error)
|
|
124
|
+
return adminCheck.error;
|
|
125
|
+
const { searchParams } = new URL(request.url);
|
|
126
|
+
const search = searchParams.get('search');
|
|
127
|
+
const tier = searchParams.get('tier');
|
|
128
|
+
const page = parseInt(searchParams.get('page') || '1');
|
|
129
|
+
const pageSize = parseInt(searchParams.get('pageSize') || '50');
|
|
130
|
+
const queryBody = {
|
|
131
|
+
page,
|
|
132
|
+
pageSize,
|
|
133
|
+
orderBy: 'created_at',
|
|
134
|
+
orderDirection: 'desc',
|
|
135
|
+
};
|
|
136
|
+
const conditions = [];
|
|
137
|
+
if (search) {
|
|
138
|
+
conditions.push({
|
|
139
|
+
operator: 'or',
|
|
140
|
+
conditions: [
|
|
141
|
+
{ field: 'email', operator: 'like', value: `%${search}%` },
|
|
142
|
+
{ field: 'display_name', operator: 'like', value: `%${search}%` },
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
if (tier) {
|
|
147
|
+
conditions.push({ field: 'tier', operator: 'eq', value: tier });
|
|
148
|
+
}
|
|
149
|
+
if (conditions.length === 1) {
|
|
150
|
+
queryBody.filter = conditions[0];
|
|
151
|
+
}
|
|
152
|
+
else if (conditions.length > 1) {
|
|
153
|
+
queryBody.filter = { operator: 'and', conditions };
|
|
154
|
+
}
|
|
155
|
+
const result = await vibeServiceRequest('/v1/collections/vibe_app/tables/users/query', { method: 'POST', body: queryBody });
|
|
156
|
+
if (!result.ok) {
|
|
157
|
+
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
158
|
+
}
|
|
159
|
+
const rawUsers = result.data?.data || result.data?.documents || [];
|
|
160
|
+
const users = rawUsers.map((u) => ({
|
|
161
|
+
id: u.id || u.document_id,
|
|
162
|
+
idp_user_id: u.idp_user_id,
|
|
163
|
+
email: u.email,
|
|
164
|
+
display_name: u.display_name || u.name,
|
|
165
|
+
tier: u.tier || 'free',
|
|
166
|
+
credits: u.credits || 0,
|
|
167
|
+
status: u.status || 'active',
|
|
168
|
+
created_at: u.created_at,
|
|
169
|
+
last_login: u.last_login || u.updated_at,
|
|
170
|
+
}));
|
|
171
|
+
return server_1.NextResponse.json({
|
|
172
|
+
users,
|
|
173
|
+
meta: result.data?.meta || { total: users.length, page, pageSize },
|
|
174
|
+
});
|
|
175
|
+
},
|
|
176
|
+
async POST(request) {
|
|
177
|
+
const adminCheck = await checkAdminRole(request);
|
|
178
|
+
if (adminCheck.error)
|
|
179
|
+
return adminCheck.error;
|
|
180
|
+
const body = await request.json();
|
|
181
|
+
const { action, userId, tier, credits } = body;
|
|
182
|
+
if (action === 'stats') {
|
|
183
|
+
const result = await vibeServiceRequest('/v1/collections/vibe_app/tables/users/query', { method: 'POST', body: { page: 1, pageSize: 10000 } });
|
|
184
|
+
if (!result.ok) {
|
|
185
|
+
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
186
|
+
}
|
|
187
|
+
const users = result.data?.data || result.data?.documents || [];
|
|
188
|
+
const now = new Date();
|
|
189
|
+
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
190
|
+
const oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
|
191
|
+
const stats = {
|
|
192
|
+
total: users.length,
|
|
193
|
+
active: users.filter((u) => u.status === 'active').length,
|
|
194
|
+
newToday: users.filter((u) => new Date(u.created_at) > oneDayAgo).length,
|
|
195
|
+
newThisWeek: users.filter((u) => new Date(u.created_at) > oneWeekAgo).length,
|
|
196
|
+
byTier: {},
|
|
197
|
+
};
|
|
198
|
+
users.forEach((u) => {
|
|
199
|
+
const t = u.tier || 'free';
|
|
200
|
+
stats.byTier[t] = (stats.byTier[t] || 0) + 1;
|
|
201
|
+
});
|
|
202
|
+
return server_1.NextResponse.json({ stats });
|
|
203
|
+
}
|
|
204
|
+
if (action === 'update_tier' && userId && tier) {
|
|
205
|
+
const result = await vibeServiceRequest(`/v1/collections/vibe_app/tables/users/${userId}`, { method: 'PUT', body: { tier } });
|
|
206
|
+
if (!result.ok) {
|
|
207
|
+
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
208
|
+
}
|
|
209
|
+
return server_1.NextResponse.json({ success: true });
|
|
210
|
+
}
|
|
211
|
+
if (action === 'update_credits' && userId && credits !== undefined) {
|
|
212
|
+
const result = await vibeServiceRequest(`/v1/collections/vibe_app/tables/users/${userId}`, { method: 'PUT', body: { credits } });
|
|
213
|
+
if (!result.ok) {
|
|
214
|
+
return server_1.NextResponse.json({ error: result.error }, { status: result.status || 500 });
|
|
215
|
+
}
|
|
216
|
+
return server_1.NextResponse.json({ success: true });
|
|
217
|
+
}
|
|
218
|
+
return server_1.NextResponse.json({ error: 'Invalid action' }, { status: 400 });
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Admin Vibe Data API Handler
|
|
3
|
-
*
|
|
4
|
-
* Provides admin-level access to Vibe data using service account credentials.
|
|
5
|
-
* Bypasses user-level filtering to allow admins to view all records.
|
|
6
|
-
*
|
|
7
|
-
* @version 1.0
|
|
8
|
-
* @requires Admin role (vibe_app_admin or payez_admin)
|
|
9
|
-
*/
|
|
10
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
11
|
-
export interface AdminVibeHandlerConfig {
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* GET /api/admin/vibe/collections
|
|
15
|
-
* List all Vibe collections
|
|
16
|
-
*/
|
|
17
|
-
export declare function createGetCollectionsHandler(config: AdminVibeHandlerConfig): (request: NextRequest) => Promise<NextResponse<any>>;
|
|
18
|
-
/**
|
|
19
|
-
* GET /api/admin/vibe/collections/[collection]/tables
|
|
20
|
-
* List tables in a collection
|
|
21
|
-
*/
|
|
22
|
-
export declare function createGetTablesHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
23
|
-
params: Promise<{
|
|
24
|
-
collection: string;
|
|
25
|
-
}>;
|
|
26
|
-
}) => Promise<NextResponse<any>>;
|
|
27
|
-
/**
|
|
28
|
-
* GET /api/admin/vibe/data/[collection]/[table]
|
|
29
|
-
* Fetch all records from a table (admin - no user filtering)
|
|
30
|
-
*/
|
|
31
|
-
export declare function createGetTableDataHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
32
|
-
params: Promise<{
|
|
33
|
-
collection: string;
|
|
34
|
-
table: string;
|
|
35
|
-
}>;
|
|
36
|
-
}) => Promise<NextResponse<any>>;
|
|
37
|
-
/**
|
|
38
|
-
* GET /api/admin/vibe/data/[collection]/[table]/[id]
|
|
39
|
-
* Fetch single record (admin access)
|
|
40
|
-
*/
|
|
41
|
-
export declare function createGetRecordHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
42
|
-
params: Promise<{
|
|
43
|
-
collection: string;
|
|
44
|
-
table: string;
|
|
45
|
-
id: string;
|
|
46
|
-
}>;
|
|
47
|
-
}) => Promise<NextResponse<any>>;
|
|
48
|
-
/**
|
|
49
|
-
* PUT /api/admin/vibe/data/[collection]/[table]/[id]
|
|
50
|
-
* Update record (admin access)
|
|
51
|
-
*/
|
|
52
|
-
export declare function createUpdateRecordHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
53
|
-
params: Promise<{
|
|
54
|
-
collection: string;
|
|
55
|
-
table: string;
|
|
56
|
-
id: string;
|
|
57
|
-
}>;
|
|
58
|
-
}) => Promise<NextResponse<any>>;
|
|
59
|
-
/**
|
|
60
|
-
* DELETE /api/admin/vibe/data/[collection]/[table]/[id]
|
|
61
|
-
* Delete record (admin access)
|
|
62
|
-
*/
|
|
63
|
-
export declare function createDeleteRecordHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
64
|
-
params: Promise<{
|
|
65
|
-
collection: string;
|
|
66
|
-
table: string;
|
|
67
|
-
id: string;
|
|
68
|
-
}>;
|
|
69
|
-
}) => Promise<NextResponse<unknown>>;
|
|
70
|
-
/**
|
|
71
|
-
* POST /api/admin/vibe/data/[collection]/[table]/query
|
|
72
|
-
* Query records with filters (admin access)
|
|
73
|
-
*/
|
|
74
|
-
export declare function createQueryHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
75
|
-
params: Promise<{
|
|
76
|
-
collection: string;
|
|
77
|
-
table: string;
|
|
78
|
-
}>;
|
|
79
|
-
}) => Promise<NextResponse<any>>;
|
|
1
|
+
/**
|
|
2
|
+
* Admin Vibe Data API Handler
|
|
3
|
+
*
|
|
4
|
+
* Provides admin-level access to Vibe data using service account credentials.
|
|
5
|
+
* Bypasses user-level filtering to allow admins to view all records.
|
|
6
|
+
*
|
|
7
|
+
* @version 1.0
|
|
8
|
+
* @requires Admin role (vibe_app_admin or payez_admin)
|
|
9
|
+
*/
|
|
10
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
11
|
+
export interface AdminVibeHandlerConfig {
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* GET /api/admin/vibe/collections
|
|
15
|
+
* List all Vibe collections
|
|
16
|
+
*/
|
|
17
|
+
export declare function createGetCollectionsHandler(config: AdminVibeHandlerConfig): (request: NextRequest) => Promise<NextResponse<any>>;
|
|
18
|
+
/**
|
|
19
|
+
* GET /api/admin/vibe/collections/[collection]/tables
|
|
20
|
+
* List tables in a collection
|
|
21
|
+
*/
|
|
22
|
+
export declare function createGetTablesHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
23
|
+
params: Promise<{
|
|
24
|
+
collection: string;
|
|
25
|
+
}>;
|
|
26
|
+
}) => Promise<NextResponse<any>>;
|
|
27
|
+
/**
|
|
28
|
+
* GET /api/admin/vibe/data/[collection]/[table]
|
|
29
|
+
* Fetch all records from a table (admin - no user filtering)
|
|
30
|
+
*/
|
|
31
|
+
export declare function createGetTableDataHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
32
|
+
params: Promise<{
|
|
33
|
+
collection: string;
|
|
34
|
+
table: string;
|
|
35
|
+
}>;
|
|
36
|
+
}) => Promise<NextResponse<any>>;
|
|
37
|
+
/**
|
|
38
|
+
* GET /api/admin/vibe/data/[collection]/[table]/[id]
|
|
39
|
+
* Fetch single record (admin access)
|
|
40
|
+
*/
|
|
41
|
+
export declare function createGetRecordHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
42
|
+
params: Promise<{
|
|
43
|
+
collection: string;
|
|
44
|
+
table: string;
|
|
45
|
+
id: string;
|
|
46
|
+
}>;
|
|
47
|
+
}) => Promise<NextResponse<any>>;
|
|
48
|
+
/**
|
|
49
|
+
* PUT /api/admin/vibe/data/[collection]/[table]/[id]
|
|
50
|
+
* Update record (admin access)
|
|
51
|
+
*/
|
|
52
|
+
export declare function createUpdateRecordHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
53
|
+
params: Promise<{
|
|
54
|
+
collection: string;
|
|
55
|
+
table: string;
|
|
56
|
+
id: string;
|
|
57
|
+
}>;
|
|
58
|
+
}) => Promise<NextResponse<any>>;
|
|
59
|
+
/**
|
|
60
|
+
* DELETE /api/admin/vibe/data/[collection]/[table]/[id]
|
|
61
|
+
* Delete record (admin access)
|
|
62
|
+
*/
|
|
63
|
+
export declare function createDeleteRecordHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
64
|
+
params: Promise<{
|
|
65
|
+
collection: string;
|
|
66
|
+
table: string;
|
|
67
|
+
id: string;
|
|
68
|
+
}>;
|
|
69
|
+
}) => Promise<NextResponse<unknown>>;
|
|
70
|
+
/**
|
|
71
|
+
* POST /api/admin/vibe/data/[collection]/[table]/query
|
|
72
|
+
* Query records with filters (admin access)
|
|
73
|
+
*/
|
|
74
|
+
export declare function createQueryHandler(config: AdminVibeHandlerConfig): (request: NextRequest, { params }: {
|
|
75
|
+
params: Promise<{
|
|
76
|
+
collection: string;
|
|
77
|
+
table: string;
|
|
78
|
+
}>;
|
|
79
|
+
}) => Promise<NextResponse<any>>;
|