Authentication
SQLPage provides multiple approaches to authenticate users in your web applications. Choose the method that best fits your security requirements and infrastructure.Cookie-Based Session Authentication
The most common authentication pattern uses cookies to maintain user sessions after login.Database Schema
First, create tables to store user accounts and sessions:User Registration
Usesqlpage.hash_password() to securely hash passwords:
Login Flow
Use theauthentication component to verify credentials:
Protecting Pages
Check for valid sessions at the top of protected pages:Logout
Clear the session cookie and database record:HTTP Basic Authentication
For simple applications or API endpoints, use HTTP Basic Auth with theauthentication component:
OpenID Connect (OIDC) Single Sign-On
SQLPage has built-in support for OIDC authentication, enabling “Login with Google”, enterprise SSO, or self-hosted identity providers like Keycloak.Configuration
Add OIDC settings to yoursqlpage/sqlpage.json:
Configuration Parameters
- oidc_issuer_url: Your OIDC provider’s base URL
- oidc_client_id: Application ID from your OIDC provider
- oidc_client_secret: Secret key for authentication (keep confidential)
- host: Your application’s web address
- oidc_protected_paths: URL paths requiring authentication (default:
["/"]protects everything) - oidc_public_paths: URL paths accessible without authentication (default:
[])
Public and Protected Pages
By default, OIDC protects your entire site. To mix public and protected pages:Accessing User Information
Retrieve authenticated user data in your SQL files:Available User Claims
Common claims available viasqlpage.user_info():
name- Full nameemail- Email addresssub- Unique user identifierpreferred_username- Usernamegiven_name,family_name- First and last names
Logout
Create a logout link using the secure OIDC logout flow:- Verifies the CSRF token
- Removes authentication cookies
- Redirects to the OIDC provider’s logout endpoint
- Returns to your homepage
Security Best Practices
Password Storage
- Always use
sqlpage.hash_password()- never store plain text passwords - The function uses Argon2id, a secure password hashing algorithm
- Each password gets a unique salt automatically
Session Security
- Use strong random session IDs:
sqlpage.random_string(32) - Set session expiration times (e.g., 1 day)
- Delete expired sessions regularly:
HTTPS
Enable HTTPS in production to protect credentials in transit:Cookie Security
For production HTTPS sites, set secure cookie flags:Examples
See complete working examples in the SQLPage repository:- Cookie-based authentication:
examples/user-authentication/ - Image gallery with auth:
examples/image gallery with user uploads/ - OIDC single sign-on:
examples/single sign on/ - CRUD with authentication:
examples/CRUD - Authentication/
Related Functions
sqlpage.hash_password()- Hash passwords securelysqlpage.random_string()- Generate session IDssqlpage.cookie()- Read cookie valuessqlpage.user_info()- Get OIDC user claimssqlpage.basic_auth_username()- Get HTTP Basic Auth usernamesqlpage.basic_auth_password()- Get HTTP Basic Auth password