[GH-ISSUE #3297] Unable to list a user session from admin user #9559

Closed
opened 2026-04-13 05:04:32 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @jpainam on GitHub (Jul 8, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3297

Hi.
I'm on an role=admin and trying to list a user session, using

 const { sessions } = await auth.api.listUserSessions({
    body: {
      userId: params.id,
    },
  });

But i'm getting

  [Error [APIError]: ] {
  status: 'UNAUTHORIZED',
  body: undefined,
  headers: {},
  statusCode: 401,
  digest: '5381'
}

I should be able to list a user session from an admin user

Here is my config

export function initAuth() {
  const config = {
    database: prismaAdapter(db, {
      provider: "postgresql",
    }),
    secret: "secret",
    emailAndPassword: {
      enabled: true,
      
    },
   
    plugins: [
      admin(),
      username(),
      nextCookies(),
    ],
  } satisfies BetterAuthOptions;

  return betterAuth(config);
}
Originally created by @jpainam on GitHub (Jul 8, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3297 Hi. I'm on an `role=admin` and trying to list a user session, using ```tsx const { sessions } = await auth.api.listUserSessions({ body: { userId: params.id, }, }); ``` But i'm getting ```tsx ⨯ [Error [APIError]: ] { status: 'UNAUTHORIZED', body: undefined, headers: {}, statusCode: 401, digest: '5381' } ``` I should be able to list a user session from an admin user Here is my config ```tsx export function initAuth() { const config = { database: prismaAdapter(db, { provider: "postgresql", }), secret: "secret", emailAndPassword: { enabled: true, }, plugins: [ admin(), username(), nextCookies(), ], } satisfies BetterAuthOptions; return betterAuth(config); } ```
GiteaMirror added the lockedbug labels 2026-04-13 05:04:32 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 8, 2025):

A 401 UNAUTHORIZED error when listing user sessions as an admin is usually caused by either the session not having the 'admin' role at runtime or the session context (like cookies or bearer token) not being sent with the API call.

First, make sure your admin role is configured correctly. If you’re customizing permissions, always spread the default admin permissions first, then add your custom permissions. For example:

const admin = ac.newRole({
  ...adminAc.statements,
  posts: ['create', 'list'],
  users: ['create', 'list'],
});

This avoids accidentally overriding required permissions for admin actions like listing sessions [source].

Also, check that your current session actually includes the 'admin' role at runtime. You can log the session or user roles before making the API call to confirm.

Finally, ensure the authentication context (cookies or bearer token) is being sent with the API call, especially if you’re calling this from a server-side or isolated environment. If the session context is missing or the user isn’t recognized as admin, you’ll get a 401 error [source].

If you’ve checked all of the above and still see the error, enabling debug logging or inspecting middleware output can help pinpoint where the authorization check is failing.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

<!-- gh-comment-id:3048110145 --> @dosubot[bot] commented on GitHub (Jul 8, 2025): <!-- Answer --> A 401 UNAUTHORIZED error when listing user sessions as an admin is usually caused by either the session not having the 'admin' role at runtime or the session context (like cookies or bearer token) not being sent with the API call. First, make sure your admin role is configured correctly. If you’re customizing permissions, always spread the default admin permissions first, then add your custom permissions. For example: ```ts const admin = ac.newRole({ ...adminAc.statements, posts: ['create', 'list'], users: ['create', 'list'], }); ``` This avoids accidentally overriding required permissions for admin actions like listing sessions [[source]](https://github.com/better-auth/better-auth/issues/2762). Also, check that your current session actually includes the 'admin' role at runtime. You can log the session or user roles before making the API call to confirm. Finally, ensure the authentication context (cookies or bearer token) is being sent with the API call, especially if you’re calling this from a server-side or isolated environment. If the session context is missing or the user isn’t recognized as admin, you’ll get a 401 error [[source]](https://github.com/better-auth/better-auth/blob/c483fa14db62b3a8d82049a167f9933c0542af7d/packages/better-auth/src/plugins/admin/access/index.ts). If you’ve checked all of the above and still see the error, enabling debug logging or inspecting middleware output can help pinpoint where the authorization check is failing. <!-- 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/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/91c550f4-96d5-4f0c-a70a-4eff8386a468?feedback_type=other)</sup>&nbsp;&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/3297)
Author
Owner

@jpainam commented on GitHub (Jul 8, 2025):

Using

const { sessions } = await auth.api.listUserSessions({
    body: {
      userId: params.id,
    },
    headers: await headers(),
  });

fixed the issue. always pass the headers when using auth.api
`

<!-- gh-comment-id:3049136449 --> @jpainam commented on GitHub (Jul 8, 2025): Using ```tsx const { sessions } = await auth.api.listUserSessions({ body: { userId: params.id, }, headers: await headers(), }); ``` fixed the issue. always pass the headers when using `auth.api` `
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9559