Overview
S-Parking uses Firebase Authentication with email/password authentication and custom claims for admin role management. The authentication layer is built on Firebase JS SDK v10.7.1.Email/Password
Standard authentication with email verification
Custom Claims
Admin privileges via Firebase custom claims
Session Management
Token-based sessions with auto-refresh
Password Reset
Built-in password recovery flow
Firebase Configuration
Authentication is initialized via the Firebase config injs/config/config.js:
Initialization
Authentication Methods
1. User Registration
Create a new user account with email verification:2. User Login
Authenticate with email and password, requiring email verification:3. User Logout
Sign out and clear local tokens:4. Password Reset
Send password reset email:Error Handling
All Firebase authentication errors are translated to user-friendly Spanish messages:- Email Already Exists
- Weak Password
- Invalid Credentials
Session Management
Auth State Observer
Monitor authentication state changes and auto-refresh tokens:Token Refresh
Firebase automatically refreshes tokens every hour. To force a refresh:Custom Claims (Admin Access)
S-Parking uses Firebase custom claims to implement admin privileges. Custom claims are set via Firebase Admin SDK (server-side only).Setting Admin Claims (Backend)
Checking Admin Status (Client)
Admin-Protected Features
Features that require admin access:Create Parking Spots
Add new spots to the system
Delete Parking Spots
Remove spots from the database
Manage Zones
Create, update, and delete zones
View Analytics
Access occupancy history and reports
Client-Side Admin Check Example
Firestore Security Rules
Firestore rules enforce authentication and admin checks server-side:Login Page Implementation
Example login page integration:Protected Routes
Implement route protection in the dashboard:Security Best Practices
Require Email Verification
Block login until email is verified to prevent fake accounts.
Use HTTPS Only
Firebase Hosting enforces HTTPS automatically.
Implement Rate Limiting
Firebase Auth has built-in rate limiting for failed attempts.
Server-Side Validation
Always verify tokens server-side before granting access.
Rotate API Keys
Rotate Firebase API keys periodically via Firebase Console.
Monitor Auth Logs
Review authentication logs in Firebase Console regularly.
Token Validation in Cloud Functions
Validate Firebase tokens in Cloud Functions for protected endpoints:Creating Admin Users
To create your first admin user:- Register via web UI: Create account at
/login.html - Get User ID: Find UID in Firebase Console > Authentication > Users
- Set Custom Claim: Run via Firebase CLI or Cloud Function:
- Force Token Refresh: User must log out and log back in for claims to take effect
Troubleshooting
- Token Expired
- Custom Claims Not Working
- CORS Errors
Issue: “Token has expired” errorSolution: Force token refresh