-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
179 lines (159 loc) · 6.23 KB
/
Copy pathauth.js
File metadata and controls
179 lines (159 loc) · 6.23 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
// auth.js
import { auth, googleProvider, db } from './firebase-config.js';
import {
signInWithPopup,
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
onAuthStateChanged,
signOut,
sendPasswordResetEmail,
sendEmailVerification
} from "https://www.gstatic.com/firebasejs/10.8.1/firebase-auth.js";
import { doc, setDoc, getDoc } from "https://www.gstatic.com/firebasejs/10.8.1/firebase-firestore.js";
import { showToast } from './components/toast.js';
// DOM Elements — resolved lazily to handle module load timing
function getAuthOverlay() { return document.getElementById('authOverlay'); }
function getAppShell() { return document.getElementById('app'); }
// Auth State Observer
onAuthStateChanged(auth, async (user) => {
const authOverlay = getAuthOverlay();
const appShell = getAppShell();
// Prevent auto-login flow during explicit registration
if (user && window.isRegistering) {
return;
}
if (user) {
window.currentUser = user;
// Hide auth, show app
if (authOverlay) {
authOverlay.style.opacity = '0';
authOverlay.style.pointerEvents = 'none';
setTimeout(() => { authOverlay.style.display = 'none'; }, 500);
}
if (appShell) {
appShell.style.display = 'flex';
setTimeout(() => appShell.classList.add('ready'), 50);
}
// Create baseline profile if not exists
await ensureUserProfile(user);
// Call global func to refresh UI
if(window.initDashboard) window.initDashboard(user);
} else {
window.currentUser = null;
// Hide app, show auth
if (appShell) {
appShell.classList.remove('ready');
appShell.style.display = 'none';
}
if (authOverlay) {
authOverlay.style.display = 'grid';
authOverlay.style.pointerEvents = 'auto';
setTimeout(() => { authOverlay.style.opacity = '1'; }, 50);
}
}
});
async function ensureUserProfile(user) {
try {
const userRef = doc(db, 'users', user.uid);
const docSnap = await getDoc(userRef);
if (!docSnap.exists()) {
await setDoc(userRef, {
profile: {
name: user.displayName || "مستخدم جديد",
email: user.email,
createdAt: new Date().getTime(),
plan: 'free',
businessName: ''
},
settings: {
bookingLink: user.uid.substring(0, 8),
slotDuration: 30,
workHours: {
start: '09:00',
end: '18:00',
slotDuration: 30,
workDays: [1,2,3,4,5]
}
}
});
}
} catch (err) {
console.error("Error ensuring user profile:", err);
}
}
// Register all auth functions via proxy bridge
const _handleGoogleAuth = async (e) => {
if(e) e.preventDefault();
try {
const result = await signInWithPopup(auth, googleProvider);
showToast('مرحباً ' + (result.user.displayName || '') + ' 👋', 'success');
} catch (error) {
console.error(error);
showToast('فشل تسجيل الدخول ❌', 'error');
}
};
const _handleLogout = async () => {
try {
await signOut(auth);
showToast('تم تسجيل الخروج', 'success');
window.location.reload();
} catch (e) {
showToast('حدث خطأ', 'error');
}
};
const _handleAuth = async () => {
const email = document.getElementById('aEmail').value.trim();
const password = document.getElementById('aPass').value;
if (!email || !password) { showToast('يرجى تعبئة الحقول المطلوبة', 'error'); return; }
const btn = document.getElementById('btnMainAuth');
if(btn) btn.disabled = true;
try {
await signInWithEmailAndPassword(auth, email, password);
showToast('تم تسجيل الدخول بنجاح', 'success');
} catch (error) {
const msgs = { 'auth/wrong-password': 'كلمة المرور غير صحيحة ❌', 'auth/user-not-found': 'الإيميل غير مسجل ❌', 'auth/invalid-credential': 'بيانات الدخول غير صحيحة ❌', 'auth/invalid-email': 'إيميل غير صحيح ❌' };
showToast(msgs[error.code] || 'خطأ: ' + error.code, 'error');
} finally { if(btn) btn.disabled = false; }
};
const _handleRegister = async () => {
const name = document.getElementById('rName').value.trim();
const email = document.getElementById('rEmail').value.trim();
const password = document.getElementById('rPass').value;
if (!name || !email || !password) { showToast('يرجى تعبئة كافة الحقول', 'error'); return; }
const btn = document.getElementById('btnRegister');
if(btn) btn.disabled = true;
try {
window.isRegistering = true; // prevent auto-login in observer
const result = await createUserWithEmailAndPassword(auth, email, password);
await setDoc(doc(db, 'users', result.user.uid), {
profile: { name, email, createdAt: Date.now(), plan: 'free', businessName: '' },
settings: { bookingLink: result.user.uid.substring(0, 8), slotDuration: 30 }
});
// Sign out immediately so they have to log in manually
await signOut(auth);
window.isRegistering = false;
showToast('تم إنشاء الحساب بنجاح 🎉 برجاء تسجيل الدخول', 'success');
// Switch to login tab
if (window.switchAuth) window.switchAuth('login');
// Pre-fill email
document.getElementById('aEmail').value = email;
document.getElementById('aPass').value = '';
} catch (error) {
window.isRegistering = false;
const msgs = { 'auth/email-already-in-use': 'الإيميل مستخدم مسبقاً ❌', 'auth/weak-password': 'كلمة المرور ضعيفة جداً ❌', 'auth/invalid-email': 'إيميل غير صحيح ❌' };
showToast(msgs[error.code] || 'خطأ: ' + error.code, 'error');
} finally { if(btn) btn.disabled = false; }
};
// Register via bridge (works even if modules load after HTML script)
if (window.registerFn) {
window.registerFn('handleAuth', _handleAuth);
window.registerFn('handleRegister', _handleRegister);
window.registerFn('handleGoogleAuth', _handleGoogleAuth);
window.registerFn('handleLogout', _handleLogout);
} else {
// Fallback: direct assignment
window._handleAuth = _handleAuth;
window._handleRegister = _handleRegister;
window._handleGoogleAuth = _handleGoogleAuth;
window._handleLogout = _handleLogout;
}