Context for managing user sessions in PhoenixKit.
This module provides functions for listing, viewing, and managing active user sessions. It's primarily used by the admin interface to monitor and control user sessions.
Functions
list_active_sessions/0- Get all currently active sessionslist_user_sessions/1- Get all active sessions for a specific userget_session_info/1- Get detailed information about a specific sessionrevoke_session/1- Revoke a specific session tokenrevoke_user_sessions/1- Revoke all sessions for a specific usercount_active_sessions/0- Get total count of active sessions
Session Information
Each session includes:
- User information (id, email, status)
- Session creation time
- Session token (first 8 chars for identification)
- Session age and validity status
Summary
Functions
Counts the total number of active sessions.
Gets detailed information about a specific session by token ID.
Gets session statistics including total, unique users, expired sessions etc.
Lists all currently active sessions with user information.
Lists all active sessions for a specific user.
Revokes a specific session by token ID.
Revokes all sessions for a specific user.
Functions
Counts the total number of active sessions.
Examples
iex> count_active_sessions()
15
Gets detailed information about a specific session by token ID.
Examples
iex> get_session_info("019b5704-3680-7b95-...")
%{token_uuid: "019b5704-...", user: %User{}, created_at: ~N[...], ...}
iex> get_session_info("019b5704-0000-0000-...")
nil
Gets session statistics including total, unique users, expired sessions etc.
Examples
iex> get_session_stats()
%{
total_active: 15,
unique_users: 8,
expired_sessions: 5,
sessions_today: 3
}
Lists all currently active sessions with user information.
Returns a list of maps containing session and user details.
Examples
iex> list_active_sessions()
[
%{
token_uuid: "019b5704-3680-7b95-...",
token_preview: "abc12345",
user: %User{uuid: "019b5704-...", email: "user@example.com"},
created_at: ~N[2024-01-01 12:00:00],
expires_at: ~N[2024-03-02 12:00:00],
is_current: false
}
]
Lists all active sessions for a specific user.
Examples
iex> list_user_sessions(%User{uuid: "019b5704-..."})
[%{token_uuid: "019b5704-...", user: %User{}, created_at: ~N[...], ...}]
Revokes a specific session by token ID.
Returns :ok if successful, {:error, :not_found} if session doesn't exist.
Examples
iex> revoke_session("019b5704-3680-7b95-...")
:ok
iex> revoke_session("019b5704-0000-0000-...")
{:error, :not_found}
Revokes all sessions for a specific user.
Returns the number of sessions revoked.
Examples
iex> revoke_user_sessions(%User{uuid: "019b5704-..."})
3