Repository navigation
Expand file tree
/
Copy pathadminpanel.html
More file actions
567 lines (522 loc) · 23.9 KB
/
Copy pathadminpanel.html
File metadata and controls
567 lines (522 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Panel - Perforated panel Designer</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Firebase SDK -->
<script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-auth-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore-compat.js"></script>
<!-- Admin Config - Load before main script -->
<script src="/admin-config.js"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #f1f5f9;
}
.stat-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 24px;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
color: white;
transition: transform 0.2s, box-shadow 0.2s;
}
.stat-card:hover {
transform: translateY(-2px);
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
}
.stat-card:nth-child(1) {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.stat-card:nth-child(2) {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}
.stat-card:nth-child(3) {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}
.stat-card h3 {
margin: 0 0 12px 0;
font-size: 0.875rem;
color: rgba(255,255,255,0.9);
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.stat-card .value {
font-size: 2.5rem;
font-weight: 700;
color: white;
}
.table-container {
background: white;
border-radius: 12px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
overflow: hidden;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
background: #f8fafc;
padding: 16px;
text-align: left;
font-weight: 600;
color: #475569;
border-bottom: 2px solid #e2e8f0;
font-size: 0.875rem;
text-transform: uppercase;
letter-spacing: 0.5px;
}
td {
padding: 16px;
border-bottom: 1px solid #e2e8f0;
color: #334155;
}
tbody tr {
transition: background-color 0.2s;
}
tbody tr:hover {
background-color: #f8fafc;
}
tr:last-child td {
border-bottom: none;
}
.user-email {
font-weight: 500;
color: #1e293b;
}
.download-count {
font-weight: 600;
color: #3b82f6;
}
.last-seen {
color: #64748b;
font-size: 0.875rem;
}
.badge {
display: inline-flex;
align-items: center;
padding: 4px 12px;
border-radius: 12px;
font-size: 0.75rem;
font-weight: 600;
}
.badge-online {
background: #d1fae5;
color: #065f46;
}
.badge-recent {
background: #dbeafe;
color: #1e40af;
}
.badge-offline {
background: #f3f4f6;
color: #6b7280;
}
</style>
</head>
<body>
<div class="min-h-screen bg-gray-100">
<!-- Header -->
<header class="bg-white border-b border-gray-200 shadow-sm">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-16">
<div class="flex items-center">
<h1 class="text-xl font-bold text-blue-600">Admin Panel</h1>
<span class="ml-4 text-sm text-gray-500">Perforated panel Designer</span>
</div>
<div class="flex items-center gap-4">
<span id="userEmail" class="text-sm text-gray-600"></span>
<button onclick="handleLogout()" class="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors text-sm font-medium">
Logout
</button>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Access Denied Message -->
<div id="accessDenied" class="hidden bg-red-50 border border-red-200 rounded-lg p-6 text-center">
<h2 class="text-xl font-bold text-red-800 mb-2">Access Denied</h2>
<p class="text-red-600">You must be logged in as the superadmin to access this page.</p>
<button onclick="handleLogin()" class="mt-4 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors">
Login
</button>
</div>
<!-- Loading State -->
<div id="loadingState" class="text-center py-12">
<div class="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
<p class="mt-4 text-gray-600">Checking access...</p>
</div>
<!-- Admin Content -->
<div id="adminContent" class="hidden">
<!-- Stats Cards -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
<div class="stat-card">
<h3>Total Users</h3>
<div class="value" id="totalUsers">0</div>
</div>
<div class="stat-card">
<h3>Total Downloads</h3>
<div class="value" id="totalDownloads">0</div>
</div>
<div class="stat-card">
<h3>Downloads Today</h3>
<div class="value" id="downloadsToday">0</div>
</div>
</div>
<!-- Users Table -->
<div class="table-container">
<div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
<div>
<h2 class="text-lg font-semibold text-gray-800">Users</h2>
<p class="text-sm text-gray-500 mt-1">Manage and monitor user activity</p>
</div>
<button onclick="loadAdminData()" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
</svg>
Refresh
</button>
</div>
<div class="overflow-x-auto">
<table>
<thead>
<tr>
<th>User</th>
<th>Downloads</th>
<th>Last Seen</th>
<th>Status</th>
</tr>
</thead>
<tbody id="usersTableBody">
<tr>
<td colspan="4" class="text-center text-gray-500 py-8">Loading...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</main>
</div>
<!-- Login Modal -->
<div id="loginModal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div class="bg-white rounded-lg shadow-xl p-6 w-full max-w-md">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-bold">Login Required</h2>
<button onclick="closeLoginModal()" class="text-gray-400 hover:text-gray-600">×</button>
</div>
<div id="loginForm">
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
<input type="email" id="loginEmail" placeholder="your@email.com" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
</div>
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700 mb-2">Password</label>
<input type="password" id="loginPassword" placeholder="Password" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
</div>
<div id="loginError" class="hidden text-red-600 text-sm mb-4"></div>
<button onclick="handleEmailLogin()" class="w-full px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors mb-2">
Login
</button>
<button onclick="handleGoogleLogin()" class="w-full px-4 py-2 bg-white border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors">
Login with Google
</button>
</div>
</div>
</div>
<script>
// Inject environment variables (set by Cloudflare Pages)
// For local development, these can be set in a config file
if (typeof window.SUPERADMIN_EMAIL === 'undefined') {
// Try to get from meta tag or use default
const metaEmail = document.querySelector('meta[name="superadmin-email"]');
window.SUPERADMIN_EMAIL = metaEmail ? metaEmail.getAttribute('content') : null;
}
// Firebase Configuration
const firebaseConfig = {
apiKey: "AIzaSyA__FJwWy6VcRyWS1cO3Jmol6BcJSEiVdA",
authDomain: "ehsanmo.firebaseapp.com",
projectId: "ehsanmo",
storageBucket: "ehsanmo.firebasestorage.app",
messagingSenderId: "287723741565",
appId: "1:287723741565:web:4be1f44d27dbe60f48ac14",
measurementId: "G-347B8XCFLX"
};
// Initialize Firebase (defensive: handles case where wrong app was initialized)
if (typeof firebase !== 'undefined') {
try {
if (firebase.apps.length > 0) {
const existingApp = firebase.apps[0];
const existingOptions = existingApp.options || {};
if (existingOptions.projectId !== firebaseConfig.projectId) {
console.warn('AdminPanel: Firebase was initialized with wrong project:', existingOptions.projectId, '- deleting and reinitializing');
existingApp.delete();
}
}
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
} catch (error) {
console.error('Firebase initialization error in adminpanel:', error);
}
}
// Get superadmin email from config
// Priority: 1. window.SUPERADMIN_EMAIL (from env/build), 2. window.ADMIN_CONFIG, 3. null
function getSuperAdminEmail() {
// First try environment variable (set by Cloudflare Pages build process)
if (window.SUPERADMIN_EMAIL) {
return window.SUPERADMIN_EMAIL;
}
// Then try config file
if (window.ADMIN_CONFIG && window.ADMIN_CONFIG.SUPERADMIN_EMAIL) {
return window.ADMIN_CONFIG.SUPERADMIN_EMAIL;
}
return null;
}
const ADMIN_EMAIL = getSuperAdminEmail();
if (!ADMIN_EMAIL) {
console.error('SUPERADMIN_EMAIL not configured!');
document.body.innerHTML = '<div style="padding: 20px; text-align: center;"><h1>Configuration Error</h1><p>SUPERADMIN_EMAIL is not configured. Please set it in admin-config.js or as an environment variable.</p></div>';
}
// Auth Manager
const authManager = {
auth: null,
db: null,
currentUser: null,
init() {
if (typeof firebase === 'undefined') {
console.error('Firebase not loaded');
return;
}
this.auth = firebase.auth();
this.db = firebase.firestore();
// Listen for auth state changes
this.auth.onAuthStateChanged((user) => {
this.currentUser = user;
checkAccess();
});
},
async signInWithEmail(email, password) {
try {
await this.auth.signInWithEmailAndPassword(email, password);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
},
async signInWithGoogle() {
try {
const provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
await this.auth.signInWithPopup(provider);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
},
async signOut() {
try {
await this.auth.signOut();
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
};
// Check if user has access
function checkAccess() {
const loadingState = document.getElementById('loadingState');
const accessDenied = document.getElementById('accessDenied');
const adminContent = document.getElementById('adminContent');
const userEmail = document.getElementById('userEmail');
if (!authManager.currentUser) {
loadingState.classList.add('hidden');
accessDenied.classList.remove('hidden');
adminContent.classList.add('hidden');
return;
}
const email = authManager.currentUser.email;
userEmail.textContent = email;
if (email.toLowerCase() === ADMIN_EMAIL.toLowerCase()) {
// User has access
loadingState.classList.add('hidden');
accessDenied.classList.add('hidden');
adminContent.classList.remove('hidden');
loadAdminData();
} else {
// Access denied
loadingState.classList.add('hidden');
accessDenied.classList.remove('hidden');
adminContent.classList.add('hidden');
}
}
// Load admin data
async function loadAdminData() {
if (!authManager.db) return;
try {
// Get total users
const usersSnapshot = await authManager.db.collection('users').get();
document.getElementById('totalUsers').textContent = usersSnapshot.size;
// Get total downloads
const downloadsSnapshot = await authManager.db.collection('downloads').get();
document.getElementById('totalDownloads').textContent = downloadsSnapshot.size;
// Get downloads today
const today = new Date();
today.setHours(0, 0, 0, 0);
const todayDownloads = downloadsSnapshot.docs.filter(doc => {
const timestamp = doc.data().timestamp;
if (timestamp && timestamp.toDate) {
return timestamp.toDate() >= today;
}
return false;
});
document.getElementById('downloadsToday').textContent = todayDownloads.length;
// Populate users table
const tbody = document.getElementById('usersTableBody');
tbody.innerHTML = '';
if (usersSnapshot.empty) {
tbody.innerHTML = '<tr><td colspan="4" class="text-center text-gray-500 py-8">No users found</td></tr>';
} else {
// Sort users by last login (most recent first)
const users = [];
usersSnapshot.forEach(doc => {
const user = doc.data();
users.push({
...user,
id: doc.id,
lastLoginTime: user.lastLogin ? user.lastLogin.toDate() : null
});
});
users.sort((a, b) => {
if (!a.lastLoginTime && !b.lastLoginTime) return 0;
if (!a.lastLoginTime) return 1;
if (!b.lastLoginTime) return -1;
return b.lastLoginTime - a.lastLoginTime;
});
users.forEach(user => {
const row = tbody.insertRow();
// Email cell
const emailCell = row.insertCell(0);
emailCell.className = 'user-email';
emailCell.innerHTML = `
<div class="flex items-center gap-3">
<div class="w-10 h-10 rounded-full bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center text-white font-semibold">
${(user.email || 'U')[0].toUpperCase()}
</div>
<div>
<div class="font-medium">${user.email || 'N/A'}</div>
${user.displayName ? `<div class="text-xs text-gray-500">${user.displayName}</div>` : ''}
</div>
</div>
`;
// Downloads cell
const downloadsCell = row.insertCell(1);
downloadsCell.className = 'download-count';
downloadsCell.textContent = user.downloadCount || 0;
// Last Seen cell
const lastSeenCell = row.insertCell(2);
lastSeenCell.className = 'last-seen';
if (user.lastLoginTime) {
const now = new Date();
const diff = now - user.lastLoginTime;
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
let timeAgo;
if (minutes < 1) {
timeAgo = 'Just now';
} else if (minutes < 60) {
timeAgo = `${minutes} min ago`;
} else if (hours < 24) {
timeAgo = `${hours} hour${hours > 1 ? 's' : ''} ago`;
} else if (days < 7) {
timeAgo = `${days} day${days > 1 ? 's' : ''} ago`;
} else {
timeAgo = user.lastLoginTime.toLocaleDateString();
}
lastSeenCell.innerHTML = `
<div>${timeAgo}</div>
<div class="text-xs text-gray-400">${user.lastLoginTime.toLocaleString()}</div>
`;
} else {
lastSeenCell.textContent = 'Never';
}
// Status cell
const statusCell = row.insertCell(3);
if (user.lastLoginTime) {
const now = new Date();
const diff = now - user.lastLoginTime;
const minutes = Math.floor(diff / 60000);
if (minutes < 15) {
statusCell.innerHTML = '<span class="badge badge-online">Online</span>';
} else if (minutes < 1440) { // 24 hours
statusCell.innerHTML = '<span class="badge badge-recent">Recent</span>';
} else {
statusCell.innerHTML = '<span class="badge badge-offline">Offline</span>';
}
} else {
statusCell.innerHTML = '<span class="badge badge-offline">Never</span>';
}
});
}
} catch (e) {
console.error('Failed to load admin data:', e);
document.getElementById('usersTableBody').innerHTML =
'<tr><td colspan="4" class="text-center text-red-500 py-8">Error loading data. Please try again.</td></tr>';
}
}
// Login functions
function handleLogin() {
document.getElementById('loginModal').classList.remove('hidden');
}
function closeLoginModal() {
document.getElementById('loginModal').classList.add('hidden');
document.getElementById('loginError').classList.add('hidden');
}
async function handleEmailLogin() {
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;
const errorEl = document.getElementById('loginError');
if (!email || !password) {
errorEl.textContent = 'Please enter email and password';
errorEl.classList.remove('hidden');
return;
}
const result = await authManager.signInWithEmail(email, password);
if (result.success) {
closeLoginModal();
} else {
errorEl.textContent = result.error;
errorEl.classList.remove('hidden');
}
}
async function handleGoogleLogin() {
const errorEl = document.getElementById('loginError');
const result = await authManager.signInWithGoogle();
if (result.success) {
closeLoginModal();
} else {
errorEl.textContent = result.error;
errorEl.classList.remove('hidden');
}
}
async function handleLogout() {
await authManager.signOut();
window.location.reload();
}
// Initialize on page load
window.addEventListener('DOMContentLoaded', () => {
authManager.init();
});
</script>
</body>
</html>