-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
91 lines (81 loc) · 3.79 KB
/
admin.js
File metadata and controls
91 lines (81 loc) · 3.79 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.8.0/firebase-app.js";
import { getAuth, onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/10.8.0/firebase-auth.js";
import { getFirestore, collection, getDocs, deleteDoc, doc } from "https://www.gstatic.com/firebasejs/10.8.0/firebase-firestore.js";
// Aapka Firebase Config
const firebaseConfig = {
apiKey: "AIzaSyCMwFJfbkdFjxWzNhMccXs9FbhqntSKdRM",
authDomain: "portfolio-auth-19a5e.firebaseapp.com",
projectId: "portfolio-auth-19a5e",
storageBucket: "portfolio-auth-19a5e.firebasestorage.app",
messagingSenderId: "211741915178",
appId: "1:211741915178:web:b96f48f37ef2477b83ab53"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
// --- Logout Logic (Fixed for Button) ---
const logoutBtn = document.getElementById('logoutBtn');
if (logoutBtn) {
logoutBtn.addEventListener('click', () => {
signOut(auth).then(() => {
// Logout ke baad hamesha login page (index.html) par bhejna chahiye
window.location.href = "index.html";
}).catch((error) => {
console.error("Logout Error:", error);
alert("Logout fail ho gaya!");
});
});
}
// --- Auth Check & Data Load ---
onAuthStateChanged(auth, (user) => {
if (user && user.email === "rajaalinagar99@gmail.com") {
console.log("Admin Verified: " + user.email);
fetchUsers();
} else {
// Agar admin nahi hai toh wapas bhej do
window.location.href = "index.html";
}
});
// --- Table Data Load Logic ---
async function fetchUsers() {
const tableBody = document.getElementById('userTable');
if (!tableBody) return;
tableBody.innerHTML = "<tr><td colspan='5' style='text-align:center; padding:20px;'>Loading Users...</td></tr>";
try {
const querySnapshot = await getDocs(collection(db, "userProfiles"));
tableBody.innerHTML = ""; // Clear loader
if (querySnapshot.empty) {
tableBody.innerHTML = "<tr><td colspan='5' style='text-align:center; padding:20px;'>Database khali hai!</td></tr>";
return;
}
querySnapshot.forEach((docSnap) => {
const user = docSnap.data();
const row = `
<tr style="border-bottom: 1px solid rgba(255,255,255,0.1);">
<td style="padding:12px;"><img src="${user.profilePic || ''}" style="width:45px; height:45px; border-radius:50%; object-fit:cover; border:1px solid #00f2fe;"></td>
<td style="padding:12px;">${user.username || 'No Name'}</td>
<td style="padding:12px; font-size:12px; color:#94a3b8;">${user.email || 'No Email'}</td>
<td style="padding:12px;">${user.aadhar || 'No Aadhar'}</td>
<td style="padding:12px;">
<button onclick="window.deleteUser('${docSnap.id}')" style="background:#ef4444; color:white; border:none; padding:6px 12px; border-radius:4px; cursor:pointer;">Delete</button>
</td>
</tr>
`;
tableBody.innerHTML += row;
});
} catch (error) {
console.error("Fetch Error:", error);
tableBody.innerHTML = "<tr><td colspan='5' style='text-align:center; color:red;'>Data nahi dikh raha? Firebase Rules check karein.</td></tr>";
}
}
// Global Delete Function
window.deleteUser = async (id) => {
if (confirm("Kya aap sach mein is user ko delete karna chahte hain?")) {
try {
await deleteDoc(doc(db, "userProfiles", id));
fetchUsers(); // Refresh table
} catch (e) {
alert("Delete failed: " + e.message);
}
}
};