Storage Permission Debugging
Current Changes and Debug Information
We’ve made several changes to fix the “Firebase Storage: User does not have permission to access ‘settings/company-logo’” error:
-
Modified the
uploadCompanyLogofunction:- Now uploads to a unique path
settings/logo-{timestamp}to avoid conflicts - Adds extensive logging to help debug permissions issues
- Shows custom claims information when permission errors occur
- Now uploads to a unique path
-
Updated Storage Rules:
- Made the
settingsfolder accessible to all authenticated users - Added simpler helper functions for checking permissions
- Made the rules more permissive for debugging purposes
- Made the
-
Improved
deleteCompanyLogofunction:- Now properly extracts path from the stored URL
- More robust error handling
- Better debugging information
How to Deploy These Changes
For the changes to take effect, you need to deploy the updated storage rules:
firebase deploy --only storageUnderstanding and Fixing Firebase Custom Claims
The root issue is likely that Firebase Auth custom claims (which contain the user’s role) are not being properly set. To properly set up custom claims:
-
Deploy Firebase Functions:
Terminal window cd functionsnpm installfirebase deploy --only functions -
Verify Function Logs: Check Firebase Functions logs to ensure the
syncUserClaimsfunction is running successfully. -
Force a Token Refresh: Use the “Refresh Authentication Token” button in the Settings page.
-
Check Current User Claims: Open your browser console and run (when logged in):
firebase.auth().currentUser.getIdTokenResult(true).then(token => console.log(token.claims))You should see a
roleproperty with the valueadmin.
Temporary Workaround
The updated code now uses a different path for each logo upload (settings/logo-{timestamp}) and has more permissive storage rules. This should allow uploads to work even without proper custom claims.
Checking for Modified Files
The following files have been modified to fix this issue:
/src/lib/firebase/configService.ts- Updated upload and delete functions with better error handling/storage.rules- Made rules more permissive for authenticated users/functions/src/auth.js- Updated to work with the default database
Next Steps
Once the logo upload is working, you should:
- Review the verbose logs in the console to understand what’s happening
- Check if custom claims are properly set in the Firebase Auth token
- Consider setting up a more secure implementation with proper role-based authentication
Remember that the current solution prioritizes getting the feature working by temporarily relaxing security rules. For production, you should restore stricter rules after ensuring custom claims are working properly.