Authentication
AxumKit uses session-based authentication with Redis. Sessions are stored as Redis keys with sliding TTL and absolute expiration.
Session Lifecycle
Create Session (Login)
When a user logs in (email/password or OAuth), a session is created:
- Generate a random session ID
- Store session data in Redis:
session:{session_id} - Set sliding TTL (default: 168 hours) and max lifetime (default: 720 hours)
- Set
session_idcookie on the response
Session Refresh (Sliding Expiration)
On each authenticated request, the session TTL is conditionally refreshed:
- If remaining TTL is below
AUTH_SESSION_REFRESH_THRESHOLD% of the sliding TTL, extend it - This avoids unnecessary Redis writes on every request
- Max lifetime (
max_expires_at) is never extended — it's an absolute limit
Session Extraction
Handlers use extractor types to access the session:
rust
// Requires authentication — returns 401 if no valid session
pub async fn protected_handler(
RequiredSession(session): RequiredSession,
) -> Result<Json<Response>, Errors> {
let user_id = session.user_id;
// ...
}
// Works with or without authentication
pub async fn public_handler(
OptionalSession(session): OptionalSession,
) -> Result<Json<Response>, Errors> {
if let Some(session) = session {
// authenticated
}
// ...
}Logout
Logout deletes the session from Redis and clears the cookie.
Email/Password Login
POST /v0/auth/loginFlow:
- Validate email and password
- Verify password hash (Argon2)
- Check if email is verified
- If TOTP is enabled → return
totp_required: truewith a temporary token - Otherwise → create session, set cookie
User Registration
POST /v0/users- Validate handle, email, password
- Hash password with Argon2
- Create user record
- Send verification email via worker (NATS job)
Email Verification
POST /v0/auth/verify-email- User receives email with verification token
- POST the token to verify
- Sets
verified_attimestamp on the user
POST /v0/auth/resend-verification-emailResends the verification email (requires active session).
Password Reset
POST /v0/auth/forgot-password- User submits their email
- System generates a reset token, stores in Redis
- Sends reset email via worker
POST /v0/auth/reset-password- User submits the token + new password
- Validates token, hashes new password, updates user
Password Change
POST /v0/auth/change-passwordRequires active session. User provides current password and new password.
Email Change
POST /v0/auth/change-email- User requests email change (requires session + current password)
- System sends confirmation to the new email
POST /v0/auth/confirm-email-changeConfirms the email change with the token from the confirmation email.
Session Configuration
| Variable | Default | Description |
|---|---|---|
AUTH_SESSION_MAX_LIFETIME_HOURS | — | Absolute session expiration |
AUTH_SESSION_SLIDING_TTL_HOURS | — | TTL extended on activity |
AUTH_SESSION_REFRESH_THRESHOLD | — | % of TTL remaining to trigger refresh |
AUTH_EMAIL_VERIFICATION_TOKEN_EXPIRE_TIME | 1 min | Verification token TTL |
AUTH_PASSWORD_RESET_TOKEN_EXPIRE_TIME | 15 min | Reset token TTL |
AUTH_EMAIL_CHANGE_TOKEN_EXPIRE_TIME | 15 min | Email change token TTL |