@@ -63,62 +63,25 @@ export { app, auth, db };
6363// Secure way to check for admin permissions
6464// This function uses a hash comparison approach to avoid exposing the email directly
6565export const isUserAdmin = async ( email : string ) : Promise < boolean > => {
66- if ( ! db ) return false ;
67-
66+ if ( ! db ) {
67+ console . error ( "Firebase db not initialized" ) ;
68+ return false ;
69+ }
70+
6871 try {
69- // First approach: Check if user has admin role in their profile
72+ // Query the Firestore users collection to check if the user with the given email has the role "admin"
7073 const usersRef = collection ( db , 'users' ) ;
7174 const q = query ( usersRef , where ( 'email' , '==' , email ) , where ( 'role' , '==' , 'admin' ) ) ;
7275 const querySnapshot = await getDocs ( q ) ;
73-
74- if ( ! querySnapshot . empty ) {
75- return true ;
76- }
77-
78- // Second approach: Check against admins collection
79- const adminsRef = collection ( db , 'admins' ) ;
80- const adminDoc = await getDoc ( doc ( adminsRef , 'authorized_emails' ) ) ;
81-
82- if ( adminDoc . exists ( ) && adminDoc . data ( ) . emails ) {
83- return adminDoc . data ( ) . emails . includes ( email ) ;
84- }
85-
86- // Fallback to hardcoded verification (using a hashed comparison for security)
87- // This allows initial admin setup even if collections don't exist yet
88- const adminHash = 'b42a70c370ad4562dbd5166f1275324fa254299f' ; // SHA1 hash of "[email protected] " 89- const emailHash = await sha1 ( email . trim ( ) . toLowerCase ( ) ) ;
90-
91- return emailHash === adminHash ;
76+
77+ // If the query returns any documents, the user is an admin
78+ return ! querySnapshot . empty ;
9279 } catch ( error ) {
9380 console . error ( 'Error checking admin status:' , error ) ;
9481 return false ;
9582 }
9683} ;
9784
98- // Utility function to create SHA-1 hash for email comparison
99- // This prevents exposing the actual admin email in the code
100- async function sha1 ( str : string ) : Promise < string > {
101- const buffer = new TextEncoder ( ) . encode ( str ) ;
102- const hashBuffer = await crypto . subtle . digest ( 'SHA-1' , buffer ) ;
103- const hashArray = Array . from ( new Uint8Array ( hashBuffer ) ) ;
104- return hashArray . map ( b => b . toString ( 16 ) . padStart ( 2 , '0' ) ) . join ( '' ) ;
105- }
106-
107- // Admin setup function (should be called once to set up the admin in Firestore)
108- export const setupAdminUser = async ( ) => {
109- if ( ! db ) return ;
110-
111- try {
112- const adminsRef = collection ( db , 'admins' ) ;
113- await setDoc ( doc ( adminsRef , 'authorized_emails' ) , {
114- 115- } ) ;
116-
117- console . log ( 'Admin setup complete' ) ;
118- } catch ( error ) {
119- console . error ( 'Error setting up admin:' , error ) ;
120- }
121- } ;
12285
12386// Auth functions with conditional checks to prevent errors
12487export const login = async ( email : string , password : string ) => {
0 commit comments