Session Management - nself-org/nchat GitHub Wiki
Complete production-ready session management with security features, device tracking, and activity monitoring.
- Device Details: Device type, browser, OS, IP address
- Location Tracking: City, country with geolocation
- Last Active: Real-time activity timestamps
- Current Session Indicator: Visual badge for current device
- Session Creation Time: Track when session was created
- Revoke Session: Sign out individual devices
- Revoke All Others: Bulk sign out all non-current sessions
- Auto-Refresh: Periodic session list updates
- Confirmation Dialogs: Safety checks before revocation
-
Device Fingerprinting: Hardware/software identification
- User agent parsing
- Screen resolution
- Timezone detection
- Language preferences
- CPU cores and memory
- Touch support detection
- WebGL renderer identification
- Remember Me Option: Extended session duration
- Automatic Tracking: Session created on login
- Geographic Location: IP-based geolocation
- Session Timeout: Configurable expiration (default 8 hours)
- Idle Timeout: Auto-logout after inactivity (default 30 minutes)
- Max Concurrent Sessions: Limit simultaneous logins (default 10)
- Session Validation: Real-time expiry checks
- Automatic Cleanup: Remove expired sessions
- New Login Alerts: Email/push on new session
- New Device Detection: Alert for unrecognized devices
- Suspicious Activity: High-risk behavior detection
- Session Revoked: Notification when signed out
- Geographic Anomaly: Alert for unusual locations
- Real-time Updates: Live notification feed
- Severity Levels: Info, warning, critical
- Unread Count Badge: Visual indicator
-
Multi-Factor Analysis:
- Rapid location changes (country switching < 6 hours)
- New device detection
- Unusual login times (3am - 6am)
- Unusual browser/OS combinations
- Multiple rapid login attempts
- Risk Scoring: 0-100 suspicious activity score
- Severity Classification: Low, medium, high, critical
- Detailed Reasoning: Explains why flagged
- Automatic Actions: Optional auto-revoke on critical
- Country Change Tracking: Detect rapid location changes
- Distance Calculation: Measure geographic movement
- Time-Based Analysis: Factor in time between logins
- Threshold Configuration: Customizable distance/time limits
- Visual Alerts: Clear warnings in UI
src/
├── lib/
│ └── auth/
│ └── session-manager.ts # Core session management logic
├── hooks/
│ └── use-sessions.ts # React hook for session operations
├── components/
│ └── settings/
│ └── SessionManagement.tsx # Main UI component
└── app/
└── api/
└── auth/
└── sessions/
├── route.ts # CRUD endpoints
└── activity/
└── route.ts # Activity tracking endpoint
class SessionManager {
// Session Creation
createSession(options: SessionCreateOptions): Promise<Session>
generateDeviceFingerprint(): Promise<DeviceFingerprint>
// Session Validation
validateSession(session: Session): SessionValidationResult
shouldRefreshSession(session: Session): boolean
updateActivity(session: Session): Session
// Security Analysis
detectSuspiciousActivity(session: Session, previousSessions: Session[]): SuspiciousActivityResult
detectGeoAnomaly(session: Session, previousSessions: Session[]): boolean
// Notifications
createNewLoginNotification(session: Session): SessionNotification
createNewDeviceNotification(session: Session): SessionNotification
createSuspiciousActivityNotification(
session: Session,
analysis: SuspiciousActivityResult
): SessionNotification
createGeoAnomalyNotification(session: Session, previousLocation: string): SessionNotification
// Enforcement
hasExceededMaxSessions(sessions: Session[]): boolean
getOldestSession(sessions: Session[]): Session | null
getSessionsToAutoRevoke(sessions: Session[]): Session[]
}const config: SessionConfig = {
// Timeouts
sessionTimeout: 480, // 8 hours
idleTimeout: 30, // 30 minutes
rememberMeDuration: 30, // 30 days
// Limits
maxConcurrentSessions: 10,
maxSessionsPerDevice: 3,
// Security
requireDeviceVerification: false,
detectSuspiciousActivity: true,
detectGeoAnomaly: true,
notifyNewLogin: true,
notifyNewDevice: true,
notifySuspiciousActivity: true,
// Thresholds
suspiciousActivityThreshold: 70,
geoAnomalyDistanceKm: 500,
}import { sessionManager } from '@/lib/auth/session-manager'
sessionManager.updateConfig({
sessionTimeout: 1440, // 24 hours
idleTimeout: 60, // 1 hour
})import { SessionManagement } from '@/components/settings/SessionManagement'
export default function SecurityPage() {
return (
<div>
<h1>Security Settings</h1>
<SessionManagement />
</div>
)
}import { useSessions } from '@/hooks/use-sessions'
function MyComponent() {
const {
sessions,
currentSession,
otherSessions,
loading,
revokeSession,
notifications,
suspiciousActivityScore,
} = useSessions()
return (
<div>
<h2>Active Sessions: {sessions.length}</h2>
<p>Security Score: {suspiciousActivityScore}/100</p>
{notifications.map((notif) => (
<Alert key={notif.id}>{notif.message}</Alert>
))}
{otherSessions.map((session) => (
<div key={session.id}>
<span>
{session.device} - {session.browser}
</span>
<button onClick={() => revokeSession(session.id)}>Sign Out</button>
</div>
))}
</div>
)
}const response = await fetch('/api/auth/sessions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
userId: 'user-123',
rememberMe: true,
ipAddress: '192.168.1.1',
deviceFingerprint: await sessionManager.generateDeviceFingerprint(),
}),
})const response = await fetch('/api/auth/sessions?userId=user-123')
const { sessions } = await response.json()const response = await fetch('/api/auth/sessions?sessionId=sess-123&userId=user-123', {
method: 'DELETE',
})const response = await fetch(
'/api/auth/sessions?userId=user-123&revokeAll=true¤tSessionId=sess-current',
{ method: 'DELETE' }
)- Sessions stored in database (PostgreSQL via Hasura)
- Session IDs are cryptographically random
- IP addresses logged for security auditing
- Location data stored as JSONB
- Hash-based identification (SHA-256)
- Non-PII data only
- Client-side generation
- Cannot be spoofed easily
- Last activity updated every 5 minutes
- Throttled to prevent spam (1 update/minute on interaction)
- Automatic cleanup on logout
- Real-time validation
- Multi-factor risk scoring
- Configurable thresholds
- Detailed logging for audit
- Optional automatic actions
- IP addresses can be anonymized
- Location data is city-level only
- User control over notifications
- GDPR-compliant data handling
-- User Sessions
CREATE TABLE nchat_user_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES nchat_users(id) ON DELETE CASCADE,
device TEXT,
browser TEXT,
os TEXT,
ip_address TEXT NOT NULL,
location JSONB,
is_current BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
last_active_at TIMESTAMPTZ DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL,
CONSTRAINT valid_expiry CHECK (expires_at > created_at)
);
CREATE INDEX idx_sessions_user_id ON nchat_user_sessions(user_id);
CREATE INDEX idx_sessions_expires_at ON nchat_user_sessions(expires_at);
CREATE INDEX idx_sessions_active ON nchat_user_sessions(user_id, expires_at)
WHERE expires_at > NOW();
-- Login History
CREATE TABLE nchat_login_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES nchat_users(id) ON DELETE SET NULL,
success BOOLEAN NOT NULL,
ip_address TEXT NOT NULL,
device TEXT,
browser TEXT,
os TEXT,
location JSONB,
failure_reason TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_login_history_user_id ON nchat_login_history(user_id);
CREATE INDEX idx_login_history_created_at ON nchat_login_history(created_at DESC);
CREATE INDEX idx_login_history_success ON nchat_login_history(user_id, success);- Simulated sessions in dev mode
- Mock data for testing UI
- No database persistence required
- Fast iteration
# Run tests
pnpm test src/lib/auth/__tests__/session-manager.test.ts
pnpm test src/hooks/__tests__/use-sessions.test.ts# Test session management flow
pnpm test:e2e e2e/session-management.spec.ts- Average session duration
- Sessions per user
- Suspicious activity rate
- Geographic distribution
- Device types distribution
- Session revocation rate
- Failed login attempts
- High suspicious activity scores
- Multiple failed logins
- Geographic anomalies
- Unusual device patterns
- Session creation failures
- Caching: Session data cached in Zustand store
- Throttling: Activity updates throttled to 1/minute
- Pagination: Login history paginated
- Indexing: Database indexes on user_id, expires_at
- Lazy Loading: Sessions loaded on-demand
- Debouncing: User interaction tracking debounced
- Handles 10,000+ sessions per user
- Sub-100ms session validation
- Efficient database queries with proper indexes
- Redis caching for high-traffic scenarios
- Check GraphQL endpoint configuration
- Verify user authentication
- Check database connectivity
- Review browser console for errors
- Confirm activity endpoint is accessible
- Check session ID validity
- Verify throttling is working correctly
- Review API logs
- Check notification permissions
- Verify suspicious activity detection is enabled
- Review notification threshold settings
- Check browser notification API
- Mobile push notifications
- Email alerts for security events
- Session recording/replay
- Advanced device verification (biometrics)
- Machine learning for anomaly detection
- Session transfer between devices
- Temporary session sharing
- Session encryption at rest
- Compliance reports (SOC2, GDPR)
- Integration with SIEM systems
- GraphQL subscriptions for real-time updates
- Batch session operations
- Session export/import
- Advanced filtering and search
- Custom notification rules
When contributing to session management:
- Security First: All changes must pass security review
- Testing Required: 100% coverage for security-critical code
- Documentation: Update this file with new features
- Performance: Benchmark new features
- Privacy: Consider GDPR/CCPA compliance
Same as parent project (MIT)