[GH-ISSUE #2319] Signout is not working with error "failed to get session" #9145

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

Originally created by @cloudfocus on GitHub (Apr 16, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2319

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Stack: Ionic v8 / React 18.3 / Better-auth 1.2.7

Better-auth is working nicely in combination with Ionic. Except for useSession and signOut.

In the client:

await authClient.signOut({
      fetchOptions: {
        onSuccess: () => {
          router.push("/sign-in"); // redirect to login page
        },
      },
    });

On the server:

app.all('/api/auth/*', async (req, res) => {
  try {
    const auth = await getAuth();
    const handler = toNodeHandler(auth);
    return await handler(req, res);
  } catch (err) {
    console.error(err);
    res.status(500).send('Internal Server Error');
  }
});

Current vs. Expected behavior

Expected: removed session and user redirection

Actual:
Server response:
{"code":"FAILED_TO_GET_SESSION","message":"Failed to get session"}

What version of Better Auth are you using?

1.2.7

Provide environment information

MacOS for PWA and Ionic
(Android mobile device OnePlus on OxygenOS 15)

Behaviour is the same on all platforms

Which area(s) are affected? (Select all that apply)

Client, Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { bearer } from "better-auth/plugins";
import { mongodbAdapter } from "better-auth/adapters/mongodb";

export const auth = betterAuth({
  database: mongodbAdapter(nativeDb),
  emailAndPassword: {  
    enabled: true
  },
  plugins: [bearer()]
});

export const authClient = createAuthClient({
  baseURL: import.meta.env.VITE_API_BASE_URL
});

Additional context

No response

Originally created by @cloudfocus on GitHub (Apr 16, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2319 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Stack: Ionic v8 / React 18.3 / Better-auth 1.2.7 Better-auth is working nicely in combination with Ionic. Except for useSession and signOut. In the client: ``` await authClient.signOut({ fetchOptions: { onSuccess: () => { router.push("/sign-in"); // redirect to login page }, }, }); ``` On the server: ``` app.all('/api/auth/*', async (req, res) => { try { const auth = await getAuth(); const handler = toNodeHandler(auth); return await handler(req, res); } catch (err) { console.error(err); res.status(500).send('Internal Server Error'); } }); ``` ### Current vs. Expected behavior Expected: removed session and user redirection Actual: Server response: `{"code":"FAILED_TO_GET_SESSION","message":"Failed to get session"}` ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash MacOS for PWA and Ionic (Android mobile device OnePlus on OxygenOS 15) Behaviour is the same on all platforms ``` ### Which area(s) are affected? (Select all that apply) Client, Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { bearer } from "better-auth/plugins"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; export const auth = betterAuth({ database: mongodbAdapter(nativeDb), emailAndPassword: { enabled: true }, plugins: [bearer()] }); export const authClient = createAuthClient({ baseURL: import.meta.env.VITE_API_BASE_URL }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 04:30:15 -05:00
Author
Owner

@Bekacru commented on GitHub (Apr 16, 2025):

"failed to get session" is returned when no session cookie is sent to the server

<!-- gh-comment-id:2810211469 --> @Bekacru commented on GitHub (Apr 16, 2025): "failed to get session" is returned when no session cookie is sent to the server
Author
Owner

@cloudfocus commented on GitHub (Apr 16, 2025):

Thanks @Bekacru for your quick response.

I've used the better fetch options like this now. The token is available at this point, so I can use it like this.

export const authClient = createAuthClient({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  fetchOptions: {
    credentials: "include",
    auth: {
      type: "Bearer",
      token: token,
    },
  }
});
<!-- gh-comment-id:2810338306 --> @cloudfocus commented on GitHub (Apr 16, 2025): Thanks @Bekacru for your quick response. I've used the better fetch options like this now. The token is available at this point, so I can use it like this. ``` export const authClient = createAuthClient({ baseURL: import.meta.env.VITE_API_BASE_URL, fetchOptions: { credentials: "include", auth: { type: "Bearer", token: token, }, } }); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9145