Session
The period of time a user is logged in and authenticated, from successful login until logout or session expiration.
What is Session?
Session is the period of time a user spends authenticated and logged in to a system, from the moment they successfully log in until they log out or the session expires.
Sessions are managed using:
- Session tokens: Stored in cookies (httpOnly, secure) or local storage
- Session state: Server-side (database, Redis) or client-side (JWT)
- Session duration: Short-lived (15-60 minutes) or long-lived (days/weeks)
Good session management is critical for security: sessions can be hijacked (stolen cookies), fixed (forcing a known session ID), or replayed (reusing expired tokens).
Analogy
Think of a session like a hotel stay. When you check in (login), you get a key card (session token) that works for a few days (session duration). After checkout (logout) or the key expires, you can't access the room anymore without checking in again.
Types and Use Cases
- Web Applications: Maintain user session across page loads using cookies
- Mobile Apps: Use refresh tokens to maintain long-lived sessions
- SSO Systems: Share session across multiple applications (SSO session)
- APIs: Use JWT access tokens (stateless sessions) for API authentication
How it Works
// Server-side Session Creation
app.post('/login', (req, res) => {
const user = authenticate(req.body.username, req.body.password);
if (user) {
// Create session
const sessionId = generateSecureRandomString();
sessions[sessionId] = {
userId: user.id,
createdAt: Date.now(),
expiresAt: Date.now() + 3600000 // 1 hour
};
// Set session cookie
res.cookie('sessionId', sessionId, {
httpOnly: true,
secure: true,
sameSite: 'strict'
});
res.json({ success: true });
}
});
// Session Validation Middleware
app.use((req, res, next) => {
const sessionId = req.cookies.sessionId;
const session = sessions[sessionId];
if (!session || session.expiresAt < Date.now()) {
return res.status(401).json({ error: 'Session expired' });
}
req.user = getUserById(session.userId);
next();
});Session vs JWT (Stateless Authentication)
Session
JWT (Stateless Authentication)
Session is stateful (server stores session)
JWT is stateless (no server storage)
Sessions require server storage (database/Redis)
JWTs can be validated without lookup
Sessions can be revoked instantly
JWTs cannot be revoked until they expire
Best Practices for Session
- Use httpOnly cookies: Prevent XSS attacks by making session cookies inaccessible to JavaScript
- Short session duration: Set session expiry to 15-60 minutes, use refresh tokens for longer sessions
- Regenerate session ID: Generate new session ID after login (prevents session fixation)
How LoginRadius Powers Session
LoginRadius CIAM platform provides flexible session management with configurable session duration (15 minutes to weeks), refresh token support for long-lived sessions, instant session revocation (logout, security events), and SSO session sharing across applications. Our platform provides session analytics dashboard, concurrent session limits, device-based session tracking, and detailed session audit logs. LoginRadius also supports JWT access tokens for stateless API authentication.
FAQs
Session-based auth stores session state on the server (database/Redis) and uses a session ID cookie. JWT auth stores all state in the token (client-side) and requires no server lookup. Sessions can be revoked instantly; JWTs cannot be revoked until they expire. Sessions are stateful; JWTs are stateless.
For high-security applications (banking): 15-30 minutes. For general web apps: 1-8 hours. For remember me functionality: days/weeks (but use a refresh token approach, not a long-lived session). Never use indefinite sessions - always set an expiration.
LoginRadius provides comprehensive session management: (1) Configurable duration - set session expiry (15 minutes to weeks), (2) Refresh tokens - maintain long-lived sessions securely, (3) Session revocation - instantly revoke sessions (logout, security events), (4) SSO sessions - share session across multiple applications, (5) Session analytics - track active sessions, login patterns, concurrent sessions per user.