Skip to content

feat(sessions): support the Query API on listSessions and listUserSessions #15621

Description

@heyrapto

Proposed Enhancement

Add Query API support to both session list endpoints, following the existing pattern used by listIdentities and listPasskeys:

  • Accept a queries parameter validated by a new Sessions query validator (mirroring Utopia\Database\Validator\Queries\Identities/Passkeys).
  • Allowed attributes: $id, $createdAt, $updatedAt, ip, countryCode, continentCode, provider, userAgent, osName, osCode, osVersion, clientType, clientName, clientCode, deviceName, deviceBrand, deviceModel, deviceId, expire, factors.
  • Resolve sessions from the sessions collection filtered by userInternalId, which is already indexed (_key_user), rather than from the embedded subquery. Apply the parsed queries to find(), and compute total from count() on the filter subset, exactly as Account/Http/Account/Identities/XList.php does.
  • Exclude expired sessions by default (expire >= now) so the endpoint matches its documentation. Callers that explicitly query expire can still retrieve history.
  • Keep the existing per-request enrichment: current, countryName, userName, and the privacy rules from project auth settings.
  • Apply the identical change to GET /v1/users/:userId/sessions. The same gap exists for GET /v1/account/mfa/factors, which could follow in a separate PR.

Expected Behavior

  • GET /v1/account/sessions and GET /v1/users/:userId/sessions accept queries and support limit, offset, orderAsc, orderDesc, equal, notEqual, greaterThan, lessThan, between, isNull, cursorAfter, and cursorBefore on the allowed attributes.
  • total reflects the number of sessions matching the filters, not the size of a capped array.
  • Expired sessions are excluded unless the caller filters on expire explicitly.
  • The current flag is still set correctly for the calling session, and only that session.
  • Requests without queries keep today's behavior and response shape, so existing SDKs and the console keep working.
  • The endpoint can be used to answer questions like "newest 10 sessions", "all sessions for deviceId = X", "all sessions from country DE", or "sign out every session from IP 1.2.3.4" without fetching the entire list.
  • Both endpoints appear in generated SDKs with the same queries/total signature as the other list methods.

Benefits

  • Brings session listing in line with every other Appwrite list endpoint, in the same service, so the SDK surface is consistent and documented the same way.
  • Makes total trustworthy and removes the silent 1000-session truncation.
  • Removes expired sessions from a response documented as "active sessions".
  • Enables device- and location-based session management in the console, dashboards, and security settings without client-side filtering of a full payload.
  • Avoids transferring and re-parsing the whole session list for projects with unlimited sessions.

🎤 Pitch

Session management is one of the few places where developers build a security-facing UI: "where am I signed in", "sign out this device", "revoke everything from this IP". Today the only way to do that is to download every session and filter it client-side, and the result can be silently incomplete once a project allows more than the subquery cap.
For example, a console page wants the 10 most recent sessions, grouped by device, with a reliable count and no expired entries. With queries support that is a single request, identical in shape to listIdentities or listPasskeys. Without it, the client has to fetch everything, guess at the ordering, filter expired rows itself, and still cannot trust total.
This is a small, contained change that reuses the existing query validator pattern and the already-indexed sessions collection, and it fixes a documented-behavior mismatch rather than adding a new concept.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

Activity

  1. added
    product / auth
    product / databases
    on Nov 14, 2026
  2. SahilArate commented on Nov 16, 2026

    @SahilArate
    Contributor

    Hi @heyrapto, i would like work on this and went through it in deep.

    i checked the code and the problem matches the issue. both session list endpoints only accept the total param. sessions are loaded through the subQuerySessions filter with a hard limit of 1000 and no expire condition, and total is just the count of that capped array.

    my solution: if i am doing any wrong pleases let me know
    -> i will add a sessions query validator (extends base) with the allowed attributes from the issue.
    -> in both GET /v1/account/sessions and GET /v1/users/:userId/sessions, i will add an optional queries param, following account/identities/XList.php. the userInternalId filter is added on the server side, find() and count() run on the sessions collection, the cursor is checked to belong to the same user, and the existing enrichment (current, countryName, userName, secret) stays unchanged.
    -> exclude expired sessions unless the caller queries expire.
    -> update parameters.lock.json and add e2e tests for filtering, ordering, pagination, total, expired sessions and the unchanged default response.
    -> leave mfa/factors for a follow-up PR, as suggested.

    i have this doubts please clear and lets discuss
    -> without a limit, find() uses its default page size (25, I believe), but today these endpoints return up to 1000 sessions. To keep the current behavior, I’d default the limit to 1000 when none is sent. Is that okay?
    -> parametersTest requires new params to be declared last, so queries would come after total. That differs from other list methods (queries, total). Is that fine?
    -> should hiding expired sessions by default apply to both endpoints, or only the account one?
    -> the issue mentions “privacy rules from project auth settings”, but I couldn’t find that logic in the current code. Where does it apply?

    i will open the PR once I’m assigned. if there is an any wrong i have done pleases let me know
    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / authproduct / databases

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions