Session Management Commands
Session management commands for setting up and managing user sessions in your Azu application.
Overview
The Azu CLI provides commands to set up and manage session storage backends. Sessions are essential for maintaining user state across HTTP requests, implementing authentication, and storing temporary user data.
Supported Backends
Azu supports three session storage backends:
Redis
Fast, in-memory key-value store
Production, high-traffic applications
Database
Persistent storage in your application database
Audit requirements, queryable sessions
Memory
In-process memory storage
Development, testing only
Commands
azu session:setup
azu session:setupConfigure and install session management for your application.
Synopsis
azu session:setup [options]Description
Sets up session management by generating configuration files, initializers, and (optionally) database migrations. The command integrates with your existing application and adds the necessary dependencies.
Options
--backend <type>
-b
Session backend: redis, memory, or database
redis
--force
-f
Overwrite existing configuration files
false
Examples
Generated Files
The setup command generates the following files:
All Backends
Database Backend Only
Setup Steps
After running the command, complete these steps:
Install dependencies:
Run migrations (database backend only):
Set environment variables:
Require the initializer in your application:
Backend-Specific Configuration
Redis Backend
Pros:
Fast performance
Automatic expiration
Scales horizontally
No database overhead
Cons:
Requires Redis server
Data is not persistent across Redis restarts (unless configured)
Configuration:
Database Backend
Pros:
Persistent storage
Queryable sessions
No additional infrastructure
Good for audit trails
Cons:
Slower than Redis
Increases database load
Requires migrations
Configuration:
Memory Backend
Pros:
No external dependencies
Fast for development
Simple setup
Cons:
Not production-safe
Sessions lost on restart
Not scalable
Single process only
Configuration:
azu session:clear
azu session:clearClear all sessions from the configured backend.
Synopsis
Description
Removes all active sessions from storage, effectively logging out all users. Use this command for maintenance, security incidents, or when changing session structure.
Options
--force
-f
Skip confirmation prompt
--backend <type>
-b
Override detected backend
Examples
Confirmation Prompt
Unless --force is specified, you'll be prompted:
Backend Detection
The command automatically detects the session backend by:
Checking
src/initializers/session.crfor store typeReading
SESSION_BACKENDenvironment variableDefaulting to Redis
Clearing Behavior by Backend
Redis
Removes all keys matching the session prefix:
Output:
Database
Executes a DELETE query on the sessions table:
Output:
Memory
Cannot be cleared remotely:
Output:
Common Workflows
Initial Setup
Switching Backends
Security Incident Response
Maintenance
Best Practices
1. Use Strong Session Secrets
Generate cryptographically secure secrets:
Never commit secrets to version control:
2. Choose the Right Backend
For Production:
High traffic: Redis
Compliance/audit: Database
Hybrid: Redis with database backup
For Development:
Memory backend for simplicity
3. Set Appropriate TTL
Balance security and user experience:
4. Monitor Session Storage
5. Implement Session Cleanup
For database backend, clean expired sessions:
Schedule with cron:
Configuration Examples
Redis with Custom Options
Database with Cleanup
Environment-Specific Configuration
Troubleshooting
Sessions Not Persisting
Check configuration:
Verify backend connectivity:
Check cookie settings:
Sessions Expiring Too Quickly
Adjust TTL:
Check Redis eviction policy:
Redis Connection Errors
Verify URL format:
Test connection:
Database Migration Issues
Ensure migration ran:
Verify table exists:
Re-run migration:
Security Considerations
1. Session Hijacking Prevention
2. Session Fixation Protection
Regenerate session ID after authentication:
3. Secure Cookie Flags
4. Session Secret Rotation
Environment Variables
SESSION_SECRET
Encryption key for session data
Yes
SESSION_BACKEND
Backend type (redis, database, memory)
No
REDIS_URL
Redis connection URL
Yes (Redis backend)
DATABASE_URL
Database connection URL
Yes (Database backend)
Related Commands
azu generate auth- Generate authentication systemazu db:migrate- Run database migrationsazu serve- Development server
See Also
Last updated