Multisession setActiveSession UNAUTHORIZED #1172

Closed
opened 2026-03-13 08:26:27 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @lukasa1993 on GitHub (May 7, 2025).

trying to set different session

const sessions = await auth.api.listDeviceSessions({ headers: request.headers });
  const activeSession = await auth.api.setActiveSession({
    body: {
      sessionToken: sessions[0].session?.token || "",
    },
    headers: request.headers,
    asResponse: true,
  });

but ti errors with

Response {
  status: 401,
  statusText: 'UNAUTHORIZED',
  headers: Headers { 'Content-Type': 'application/json' },
  body: ReadableStream { locked: false, state: 'readable', supportsBYOB: true },
  bodyUsed: false,
  ok: false,
  redirected: false,
  type: 'default',
  url: ''
}

in browser cookie i clearly see multisession cookie having with that token in name

one thing i noticed i don't seem to have active session meaning non-multi session so i guess this does some extra check that active session exists before it sets from multi session list ?

Originally created by @lukasa1993 on GitHub (May 7, 2025). trying to set different session ``` const sessions = await auth.api.listDeviceSessions({ headers: request.headers }); const activeSession = await auth.api.setActiveSession({ body: { sessionToken: sessions[0].session?.token || "", }, headers: request.headers, asResponse: true, }); ``` but ti errors with ``` Response { status: 401, statusText: 'UNAUTHORIZED', headers: Headers { 'Content-Type': 'application/json' }, body: ReadableStream { locked: false, state: 'readable', supportsBYOB: true }, bodyUsed: false, ok: false, redirected: false, type: 'default', url: '' } ``` in browser cookie i clearly see multisession cookie having with that token in name one thing i noticed i don't seem to have active session meaning non-multi session so i guess this does some extra check that active session exists before it sets from multi session list ?
Author
Owner

@frectonz commented on GitHub (Jul 15, 2025):

You might have got the UNAUTHORIZED error because the auth.api.listDeviceSessions returned an empty list. Which in turn caused sessionToken token value to be submitted as an empty string, in that case the setActiveSession function returns the 401 status code.

@frectonz commented on GitHub (Jul 15, 2025): You might have got the `UNAUTHORIZED` error because the `auth.api.listDeviceSessions` returned an empty list. Which in turn caused `sessionToken` token value to be submitted as an empty string, in that case the `setActiveSession` function returns the `401` status code.
Author
Owner

@frectonz commented on GitHub (Jul 15, 2025):

Would be great if you provide a code example, i could use to recreate the issue you faced.

@frectonz commented on GitHub (Jul 15, 2025): Would be great if you provide a code example, i could use to recreate the issue you faced.
Author
Owner

@frectonz commented on GitHub (Jul 15, 2025):

For reference, This the repo I used to try to reproduce the issue.

https://github.com/frectonz/better-auth-multi-session

@frectonz commented on GitHub (Jul 15, 2025): For reference, This the repo I used to try to reproduce the issue. https://github.com/frectonz/better-auth-multi-session
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1172