What You’ll Build
A full authentication system with:- User signup with password validation
- Secure login with password verification
- Session management with cookies
- Protected pages that require authentication
- Logout functionality

Database Schema
Create two tables: one for user credentials and one for active sessions.sqlpage/migrations/0000_init.sql
Building the Signup Form
Create the Signup Form
signup.sqlImplementing Login
Create Login Form
How Authentication Works
Password Verification
Password Verification
The authentication component compares the submitted password with the stored hash:
- If passwords don’t match: SQLPage redirects to the
linkparameter and stops execution - If passwords match: Execution continues to the next query
Session Generation
Session Generation
- The
login_sessiontable (server-side) - A browser cookie (client-side)
Cookie Storage
Cookie Storage
Creating Protected Pages
Protected pages check for a valid session before displaying content.protected_page.sql
Authentication Check Pattern
Validate Session
Query the
login_session table to check if the session ID exists and get the associated username.Implementing Logout
logout.sql
- Deletes the session from the database
- Clears the cookie by setting it to an empty value
- Redirects the user to the home page
Dynamic Navigation Based on Auth State
Show different menu items based on whether the user is logged in.index.sql
- Not logged in: Shows “signin” and “signup” links
- Logged in: Shows “logout” link
Security Best Practices
Password Hashing
Always use
sqlpage.hash_password() - never store plain text passwords. SQLPage uses bcrypt with appropriate cost factor.Secure Cookies
Set
secure to TRUE in production and use HTTPS to prevent cookie theft over insecure connections.Session Expiration
Implement session timeout by checking
created_at:CSRF Protection
SQLPage automatically includes CSRF tokens in forms. Ensure you’re using POST for state-changing operations.
Complete Application Structure
Session Management Patterns
- Basic Session Check
- With Expiration
- Reusable Component
Production Deployment Checklist
Advanced Authentication Topics
OAuth/SSO Integration
Follow the progress on OAuth and Single Sign-On support
Multi-Factor Authentication
Implement 2FA by storing and verifying TOTP codes
Password Reset Flow
Build password reset with email verification using
sqlpage.exec()Remember Me
Extend session duration with long-lived tokens