[GH-ISSUE #7281] authClient.listSessions returns duplicated sessions (same ID) #19410

Closed
opened 2026-04-15 18:28:22 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @phuctm97 on GitHub (Jan 12, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/7281

Originally assigned to: @bytaesu on GitHub.

Description

When calling authClient.listSessions(), the response contains duplicated sessions with the same session ID.

Expected Behavior

Each session should appear only once in the response, with unique session IDs.

Actual Behavior

The same session appears multiple times in the response array, with identical session IDs.

Steps to Reproduce

  1. Authenticate a user
  2. Call authClient.listSessions()
  3. Observe that the returned sessions array contains duplicates

Environment

  • better-auth version: 1.4.10
  • Plugins: bearer plugin enabled on server-side

Server Configuration

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { bearer } from "better-auth/plugins";

export const auth = betterAuth({
  trustedOrigins,
  databaseHooks: {
    user: {
      create: {
        before: async (user) => {
          // ...
        },
        after: async (user) => {
          // ...
        },
      },
    },
  },
  plugins: [bearer()],
  appName: "Example",
  baseURL: "http://localhost:3000",
  basePath: "/v1/auth",
  advanced: { cookiePrefix: "v1_auth" },
  database: drizzleAdapter(database, { provider: "pg" }),
  secondaryStorage: {
    get: (key) => redis.get(key),
    set: async (key, value, ttl) => {
      await (typeof ttl === "number"
        ? redis.set(key, value, { expiration: { type: "EX", value: ttl } })
        : redis.set(key, value));
    },
    delete: async (key) => {
      await redis.del(key);
    },
  },
  rateLimit: { storage: "secondary-storage" },
  emailVerification: {
    sendVerificationEmail: async ({ user, url }) => {
      // ...
    },
    afterEmailVerification: async (user) => {
      // ...
    },
  },
  emailAndPassword: {
    enabled: true,
    autoSignIn: true,
    sendResetPassword: async ({ user, url }) => {
      // ...
    },
  },
});

Client Configuration

import { createAuthClient } from "better-auth/client";

export const authClient = createAuthClient({
  baseURL: "http://localhost:3000",
  basePath: "/v1/auth",
  fetchOptions: {
    throw: true,
    auth: { type: "Bearer", token: getAuthToken },
    onSuccess: async ({ response }) => {
      const token = response.headers.get("set-auth-token");
      if (token) await setAuthToken(token);
    },
  },
});
Originally created by @phuctm97 on GitHub (Jan 12, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/7281 Originally assigned to: @bytaesu on GitHub. ## Description When calling `authClient.listSessions()`, the response contains duplicated sessions with the same session ID. ## Expected Behavior Each session should appear only once in the response, with unique session IDs. ## Actual Behavior The same session appears multiple times in the response array, with identical session IDs. ## Steps to Reproduce 1. Authenticate a user 2. Call `authClient.listSessions()` 3. Observe that the returned sessions array contains duplicates ## Environment - better-auth version: 1.4.10 - Plugins: `bearer` plugin enabled on server-side ## Server Configuration ```tsx import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { bearer } from "better-auth/plugins"; export const auth = betterAuth({ trustedOrigins, databaseHooks: { user: { create: { before: async (user) => { // ... }, after: async (user) => { // ... }, }, }, }, plugins: [bearer()], appName: "Example", baseURL: "http://localhost:3000", basePath: "/v1/auth", advanced: { cookiePrefix: "v1_auth" }, database: drizzleAdapter(database, { provider: "pg" }), secondaryStorage: { get: (key) => redis.get(key), set: async (key, value, ttl) => { await (typeof ttl === "number" ? redis.set(key, value, { expiration: { type: "EX", value: ttl } }) : redis.set(key, value)); }, delete: async (key) => { await redis.del(key); }, }, rateLimit: { storage: "secondary-storage" }, emailVerification: { sendVerificationEmail: async ({ user, url }) => { // ... }, afterEmailVerification: async (user) => { // ... }, }, emailAndPassword: { enabled: true, autoSignIn: true, sendResetPassword: async ({ user, url }) => { // ... }, }, }); ``` ## Client Configuration ```typescript import { createAuthClient } from "better-auth/client"; export const authClient = createAuthClient({ baseURL: "http://localhost:3000", basePath: "/v1/auth", fetchOptions: { throw: true, auth: { type: "Bearer", token: getAuthToken }, onSuccess: async ({ response }) => { const token = response.headers.get("set-auth-token"); if (token) await setAuthToken(token); }, }, }); ```
GiteaMirror added the lockedbug labels 2026-04-15 18:28:22 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 12, 2026):

It looks like you're encountering an issue where calling authClient.listSessions() is returning duplicated sessions with the same session ID. This issue might arise due to the timing of the session ID generation, which happens during createSession, not during the database insert. This means that if your sessions are not being synced properly between your secondary storage and the database, duplicates could appear.

One potential next step is to check your session handling logic and ensure that all sessions are being created and stored uniquely. Additionally, consider reviewing any logs or debugging to confirm whether sessions are being created multiple times unintentionally.

If you're still having trouble after this, it may be helpful to tag the Better Auth team members, @bekacru and @ping__, to get more direct support.

What other issues are you facing with your authentication setup?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3736904893 --> @better-auth-agent[bot] commented on GitHub (Jan 12, 2026): It looks like you're encountering an issue where calling `authClient.listSessions()` is returning duplicated sessions with the same session ID. This issue might arise due to the timing of the session ID generation, which happens during `createSession`, not during the database insert. This means that if your sessions are not being synced properly between your secondary storage and the database, duplicates could appear. One potential next step is to check your session handling logic and ensure that all sessions are being created and stored uniquely. Additionally, consider reviewing any logs or debugging to confirm whether sessions are being created multiple times unintentionally. If you're still having trouble after this, it may be helpful to tag the Better Auth team members, @bekacru and @ping__, to get more direct support. What other issues are you facing with your authentication setup? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@phuctm97 commented on GitHub (Jan 12, 2026):

Please take a look, I think this is a critical issue @Bekacru and @ping__

<!-- gh-comment-id:3736916450 --> @phuctm97 commented on GitHub (Jan 12, 2026): Please take a look, I think this is a critical issue @Bekacru and @ping__
Author
Owner

@dosubot[bot] commented on GitHub (Jan 12, 2026):

This is a known code-level issue: when using secondary storage (like Redis), the active sessions list for a user can contain duplicate tokens, and the current implementation of authClient.listSessions() does not deduplicate them. This means if the same session token is added multiple times (for example, due to concurrent writes or repeated additions), you'll see duplicate sessions with the same ID in the response. The bearer plugin and secondary storage setup are not directly responsible for deduplication—it's how the session list is managed and returned by the internal adapter. See the relevant code here.

Until an upstream fix is released, you can work around this by deduplicating sessions client-side (e.g., filter by unique session ID after calling listSessions). If you want to patch this server-side, you could filter the session list before returning it in your API handler.

Recent PRs are improving session listing robustness (like skipping invalid sessions), but deduplication of duplicate session IDs is not yet addressed (PR #7182). If you want to avoid this issue, periodically clear out duplicates in your secondary storage or ensure your session creation logic doesn't add the same token more than once.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3736917319 --> @dosubot[bot] commented on GitHub (Jan 12, 2026): <!-- Answer --> This is a known code-level issue: when using secondary storage (like Redis), the active sessions list for a user can contain duplicate tokens, and the current implementation of `authClient.listSessions()` does not deduplicate them. This means if the same session token is added multiple times (for example, due to concurrent writes or repeated additions), you'll see duplicate sessions with the same ID in the response. The bearer plugin and secondary storage setup are not directly responsible for deduplication—it's how the session list is managed and returned by the internal adapter. See the relevant code [here](https://github.com/better-auth/better-auth/blob/f0a6f9448566ffa7f40f51bc881f72448f28836f/packages/better-auth/src/db/internal-adapter.ts#L150-L342). Until an upstream fix is released, you can work around this by deduplicating sessions client-side (e.g., filter by unique session ID after calling `listSessions`). If you want to patch this server-side, you could filter the session list before returning it in your API handler. Recent PRs are improving session listing robustness (like skipping invalid sessions), but deduplication of duplicate session IDs is not yet addressed ([PR #7182](https://github.com/better-auth/better-auth/pull/7182)). If you want to avoid this issue, periodically clear out duplicates in your secondary storage or ensure your session creation logic doesn't add the same token more than once. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/04a0a6c4-b240-43fb-b22c-725924649708?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20known%20code-level%20issue%3A%20when%20using%20secondary%20storage%20%28like%20Redis%29%2C%20the%20active%20sessions%20list%20for%20a%20user%20can%20contain%20duplicate%20tokens%2C%20and%20the%20current%20implementation%20of%20%60authClient.listSessions%28%29%60%20does%20not%20deduplicate%20them.%20This%20means%20if%20the%20same%20session%20token%20is%20added%20multiple%20times%20%28for%20example%2C%20due%20to%20concurrent%20writes%20or%20repeated%20additions%29%2C%20you%27ll%20see%20duplicate%20sessions%20with%20the%20same%20ID%20in%20the%20response.%20The%20bearer%20plugin%20and%20secondary%20storage%20setup%20are%20not%20directly%20responsible%20for%20deduplication%E2%80%94it%27s%20how%20the%20session%20list%20is%20managed%20and%20returned%20by%20the%20internal%20adapter.%20See%20the%20relevant%20code%20%5Bhere%5D%28https%3A//github.com/better-auth/better-auth/blob/f0a6f9448566ffa7f40f51bc881f72448f28836f/packages/better-auth/src/db/internal-adapter.ts%23L150-L342%29.%0A%0AUntil%20an%20upstream%20fix%20is%20released%2C%20you%20can%20work%20around%20this%20by%20deduplicating%20sessions%20client-side%20%28e.g.%2C%20filter%20by%20unique%20session%20ID%20after%20calling%20%60listSessions%60%29.%20If%20you%20want%20to%20patch%20this%20server-side%2C%20you%20could%20filter%20the%20session%20list%20before%20returning%20it%20in%20your%20API%20handler.%0A%0ARecent%20PRs%20are%20improving%20session%20listing%20robustness%20%28like%20skipping%20invalid%20sessions%29%2C%20but%20deduplication%20of%20duplicate%20session%20IDs%20is%20not%20yet%20addressed%20%28%5BPR%20%237182%5D%28https%3A//github.com/better-auth/better-auth/pull/7182%29%29.%20If%20you%20want%20to%20avoid%20this%20issue%2C%20periodically%20clear%20out%20duplicates%20in%20your%20secondary%20storage%20or%20ensure%20your%20session%20creation%20logic%20doesn%27t%20add%20the%20same%20token%20more%20than%20once.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/7281)
Author
Owner

@bytaesu commented on GitHub (Jan 12, 2026):

Hi @phuctm97,

I will look into this!

<!-- gh-comment-id:3737588289 --> @bytaesu commented on GitHub (Jan 12, 2026): Hi @phuctm97, I will look into this!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19410