Spaces:
Running
Running
mwdo3 commited on
مش عارف ادخل هلي صفحه الادمن . اريد اول مره يتم فيها انشاء حساب ( اول حساب يتم انشاءه ) يكون هو حساب الادمن
Browse files- admin.html +22 -6
- index.html +15 -5
admin.html
CHANGED
|
@@ -242,23 +242,39 @@
|
|
| 242 |
const userRole = document.getElementById('userRole');
|
| 243 |
const saveUserBtn = document.getElementById('saveUserBtn');
|
| 244 |
const cancelUserBtn = document.getElementById('cancelUserBtn');
|
| 245 |
-
|
| 246 |
// Check auth state and load data
|
| 247 |
auth.onAuthStateChanged(user => {
|
| 248 |
if (user) {
|
| 249 |
db.collection('users').doc(user.uid).get().then(doc => {
|
| 250 |
-
if (doc.exists
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
} else {
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
}
|
| 255 |
});
|
| 256 |
} else {
|
| 257 |
window.location.href = 'index.html';
|
| 258 |
}
|
| 259 |
});
|
| 260 |
-
|
| 261 |
-
// Load products
|
| 262 |
function loadProducts() {
|
| 263 |
db.collection('products').get().then(querySnapshot => {
|
| 264 |
productsTable.innerHTML = '';
|
|
|
|
| 242 |
const userRole = document.getElementById('userRole');
|
| 243 |
const saveUserBtn = document.getElementById('saveUserBtn');
|
| 244 |
const cancelUserBtn = document.getElementById('cancelUserBtn');
|
|
|
|
| 245 |
// Check auth state and load data
|
| 246 |
auth.onAuthStateChanged(user => {
|
| 247 |
if (user) {
|
| 248 |
db.collection('users').doc(user.uid).get().then(doc => {
|
| 249 |
+
if (doc.exists) {
|
| 250 |
+
const userData = doc.data();
|
| 251 |
+
if (userData.role === 'admin') {
|
| 252 |
+
loadProducts();
|
| 253 |
+
} else {
|
| 254 |
+
window.location.href = 'index.html';
|
| 255 |
+
}
|
| 256 |
} else {
|
| 257 |
+
// If user doc doesn't exist, check if it's the first user
|
| 258 |
+
db.collection('users').get().then(snapshot => {
|
| 259 |
+
if (snapshot.empty) {
|
| 260 |
+
// First user - automatically make admin
|
| 261 |
+
db.collection('users').doc(user.uid).set({
|
| 262 |
+
displayName: user.displayName,
|
| 263 |
+
email: user.email,
|
| 264 |
+
role: 'admin',
|
| 265 |
+
createdAt: firebase.firestore.FieldValue.serverTimestamp()
|
| 266 |
+
}).then(() => loadProducts());
|
| 267 |
+
} else {
|
| 268 |
+
window.location.href = 'index.html';
|
| 269 |
+
}
|
| 270 |
+
});
|
| 271 |
}
|
| 272 |
});
|
| 273 |
} else {
|
| 274 |
window.location.href = 'index.html';
|
| 275 |
}
|
| 276 |
});
|
| 277 |
+
// Load products
|
|
|
|
| 278 |
function loadProducts() {
|
| 279 |
db.collection('products').get().then(querySnapshot => {
|
| 280 |
productsTable.innerHTML = '';
|
index.html
CHANGED
|
@@ -361,12 +361,23 @@ productsContainer.innerHTML = products.map(product => `
|
|
| 361 |
alert(error.message);
|
| 362 |
});
|
| 363 |
}
|
| 364 |
-
|
| 365 |
function signup(name, email, password) {
|
| 366 |
auth.createUserWithEmailAndPassword(email, password)
|
| 367 |
.then(cred => {
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
});
|
| 371 |
})
|
| 372 |
.then(() => {
|
|
@@ -376,8 +387,7 @@ productsContainer.innerHTML = products.map(product => `
|
|
| 376 |
alert(error.message);
|
| 377 |
});
|
| 378 |
}
|
| 379 |
-
|
| 380 |
-
function logout() {
|
| 381 |
auth.signOut();
|
| 382 |
}
|
| 383 |
|
|
|
|
| 361 |
alert(error.message);
|
| 362 |
});
|
| 363 |
}
|
|
|
|
| 364 |
function signup(name, email, password) {
|
| 365 |
auth.createUserWithEmailAndPassword(email, password)
|
| 366 |
.then(cred => {
|
| 367 |
+
// Check if this is the first user (admin)
|
| 368 |
+
return db.collection('users').get().then(snapshot => {
|
| 369 |
+
const isAdmin = snapshot.empty;
|
| 370 |
+
return Promise.all([
|
| 371 |
+
cred.user.updateProfile({
|
| 372 |
+
displayName: name
|
| 373 |
+
}),
|
| 374 |
+
db.collection('users').doc(cred.user.uid).set({
|
| 375 |
+
displayName: name,
|
| 376 |
+
email: email,
|
| 377 |
+
role: isAdmin ? 'admin' : 'user',
|
| 378 |
+
createdAt: firebase.firestore.FieldValue.serverTimestamp()
|
| 379 |
+
})
|
| 380 |
+
]);
|
| 381 |
});
|
| 382 |
})
|
| 383 |
.then(() => {
|
|
|
|
| 387 |
alert(error.message);
|
| 388 |
});
|
| 389 |
}
|
| 390 |
+
function logout() {
|
|
|
|
| 391 |
auth.signOut();
|
| 392 |
}
|
| 393 |
|