DIYables ESP32 Web Server Authentication Example
ESP32 - Web Server with Basic Authentication
This example demonstrates how to create a secure web server with HTTP Basic Authentication on ESP32 using the DIYables_ESP32_WebServer library.
Hardware Used In This Tutorial
Or you can buy the following kits:
1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Features Demonstrated
- ✅ HTTP Basic Authentication
- ✅ Username and password protection
- ✅ Browser-native login dialogs
- ✅ Secure access control
- ✅ Simple authentication setup
- ✅ Backward compatibility (authentication optional)
Circuit Diagram
No additional wiring required - this example uses only the built-in LED and WiFi functionality of the ESP32.
Code Example
How It Works
1. Authentication Setup
The example enables HTTP Basic Authentication with:
2. Automatic Protection
Once authentication is enabled, all routes are automatically protected. Users must provide valid credentials to access any page.
3. Browser Integration
When users visit the web server:
- Browser displays a login dialog
- User enters username and password
- On success: Page loads normally
- On failure: 401 Unauthorized page is shown
4. Credential Storage
Browsers cache credentials for the session, so users don't need to log in repeatedly.
Security Considerations
✅ Suitable For:
- Home networks and trusted environments
- Internal IoT devices
- Development and prototyping
- Educational projects
⚠️ Limitations:
- Credentials are Base64 encoded, not encrypted
- No HTTPS support (ESP32 platform limitation)
- Single username/password combination
- Not suitable for production use over public networks
🔒 Best Practices:
- Change default credentials immediately
- Use only on trusted networks
- Consider additional network security (VPN, firewall)
- For high-security applications, add additional authentication layers
Testing the Authentication
- Upload the code to your ESP32
- Open Serial Monitor to see the IP address
- Visit the IP address in your web browser
- Login dialog appears - enter credentials:
- Username: admin
- Password: esp32
- Success page loads after authentication
Customization Options
Change Credentials
Custom Realm
Disable Authentication
Check Authentication Status
Backward Compatibility
Authentication is disabled by default, so existing code continues to work without modification. Enable authentication only when needed:
Troubleshooting
Browser Keeps Asking for Credentials
- Check username/password for typos
- Clear browser cache/cookies
- Verify credentials match exactly
Cannot Access Any Pages
- Authentication protects ALL routes when enabled
- Use server.disableAuthentication() to test
- Check Serial output for authentication messages
Authentication Not Working
- Ensure enableAuthentication() is called after server.begin()
- Verify credentials are within length limits (31 characters max)
- Check for special characters in passwords
Related Examples
- Web Server Example - Basic multi-page web server
- Web Server with Query Strings - Dynamic content with parameters
- Web Server with WebSocket - Real-time communication
References
- Authentication Documentation - Complete authentication reference
- DIYables_ESP32_WebServer Library Reference - Full API documentation