Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
201 lines (183 loc) · 6.89 KB
/
Copy pathauth.js
File metadata and controls
201 lines (183 loc) · 6.89 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
// Authentication and User Management
// Firebase imports will be loaded via CDN in HTML
class AuthManager {
constructor() {
this.auth = null;
this.db = null;
this.currentUser = null;
this.isAdmin = false;
this.adminUsername = import.meta.env?.VITE_ADMIN_USERNAME || 'admin';
this.adminPassword = import.meta.env?.VITE_ADMIN_PASSWORD || 'admin123';
}
async init(firebaseConfig) {
// Initialize Firebase (will be done via CDN in HTML)
return new Promise((resolve) => {
if (window.firebase) {
try {
// Check if already initialized with a different config
if (firebase.apps.length > 0) {
const existingApp = firebase.apps[0];
const existingOptions = existingApp.options || {};
if (existingOptions.projectId !== firebaseConfig.projectId) {
console.warn('AuthManager: Firebase was initialized with wrong project:', existingOptions.projectId);
}
}
// Only initialize if not already done
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
} catch (error) {
console.error('Firebase initialization error in AuthManager:', error);
}
this.auth = firebase.auth();
this.db = firebase.firestore();
// Listen for auth state changes
this.auth.onAuthStateChanged((user) => {
this.currentUser = user;
this.checkAdminStatus();
this.updateUI();
resolve();
});
} else {
resolve();
}
});
}
async checkAdminStatus() {
if (!this.currentUser) {
this.isAdmin = false;
return;
}
// Check if user is admin via custom claim or Firestore
try {
const userDoc = await this.db.collection('users').doc(this.currentUser.uid).get();
this.isAdmin = userDoc.exists && userDoc.data().isAdmin === true;
} catch (e) {
this.isAdmin = false;
}
}
async signInWithEmail(email, password) {
try {
await this.auth.signInWithEmailAndPassword(email, password);
await this.trackLogin();
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async signUpWithEmail(email, password) {
try {
const userCredential = await this.auth.createUserWithEmailAndPassword(email, password);
// Create user document
await this.db.collection('users').doc(userCredential.user.uid).set({
email: email,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
isAdmin: false,
downloadCount: 0
});
await this.trackLogin();
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async signInWithGoogle() {
try {
const provider = new firebase.auth.GoogleAuthProvider();
const userCredential = await this.auth.signInWithPopup(provider);
// Create user document if doesn't exist
const userDoc = await this.db.collection('users').doc(userCredential.user.uid).get();
if (!userDoc.exists) {
await this.db.collection('users').doc(userCredential.user.uid).set({
email: userCredential.user.email,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
isAdmin: false,
downloadCount: 0
});
}
await this.trackLogin();
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async resetPassword(email) {
try {
await this.auth.sendPasswordResetEmail(email);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async signOut() {
try {
await this.auth.signOut();
this.currentUser = null;
this.isAdmin = false;
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async trackLogin() {
if (!this.currentUser) return;
try {
await this.db.collection('loginLogs').add({
userId: this.currentUser.uid,
email: this.currentUser.email,
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
} catch (e) {
console.error('Failed to track login:', e);
}
}
async trackDownload(fileType) {
if (!this.currentUser) return;
try {
// Track download
await this.db.collection('downloads').add({
userId: this.currentUser.uid,
email: this.currentUser.email,
fileType: fileType,
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
// Update user download count
await this.db.collection('users').doc(this.currentUser.uid).update({
downloadCount: firebase.firestore.FieldValue.increment(1)
});
} catch (e) {
console.error('Failed to track download:', e);
}
}
async adminLogin(username, password) {
if (username === this.adminUsername && password === this.adminPassword) {
// Set admin session
sessionStorage.setItem('adminSession', 'true');
this.isAdmin = true;
return { success: true };
}
return { success: false, error: 'Invalid admin credentials' };
}
checkAdminSession() {
const adminSession = sessionStorage.getItem('adminSession');
if (adminSession === 'true') {
this.isAdmin = true;
}
}
updateUI() {
// This will be called from main script to update UI based on auth state
if (typeof window.updateAuthUI === 'function') {
window.updateAuthUI(this.currentUser, this.isAdmin);
}
}
canDownload(fileType) {
// Guest users can only download PNG and TXT
if (!this.currentUser) {
return fileType === 'png' || fileType === 'txt';
}
// Registered users can download all formats
return true;
}
}
// Export for use in main script
window.AuthManager = AuthManager;