session is null after logging in #1151

Closed
opened 2026-03-13 08:25:15 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @weepy on GitHub (May 2, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I set up the basic better-auth sveltekit app.

Current vs. Expected behavior

After logging in, I can see the correct user etc in the database.

But there session is null, whether I use getSession, useSession or else.

What version of Better Auth are you using?

lastest

Provide environment information

- Macos M1
- Node
- Sveltekit latest
-

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import Database from "better-sqlite3";
 
import { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } from "$env/static/private";
import { BETTER_AUTH_URL, BETTER_AUTH_SECRET, SQLITE_DB_PATH } from "$env/static/private";

const SqliteDb = new Database(SQLITE_DB_PATH);

export const auth = betterAuth({
    baseURL: BETTER_AUTH_URL,
    secret: BETTER_AUTH_SECRET,
    database: SqliteDb,

    socialProviders: { 
        github: { 
           clientId: GITHUB_CLIENT_ID, 
           clientSecret: GITHUB_CLIENT_SECRET, 
        }, 

    } 
})

Additional context

Hooks

import { auth } from "$lib/auth"; // path to your auth file
import { svelteKitHandler } from "better-auth/svelte-kit";

export async function handle({ event, resolve }) {

return svelteKitHandler({ event, resolve, auth });

}

Page

<script lang="ts"> import { authClient } from "$lib/auth-client"; const session = authClient.useSession(); </script>
  <div>
    {#if $session.data}
      <div>
        <p>
          {$session?.data?.user.name}
        </p>
        <button
          on:click={async () => {
            await authClient.signOut();
          }}
        >
          Sign Out
        </button>
      </div>
    {:else}
      <button
        on:click={async () => {
          await authClient.signIn.social({
            provider: "github",
          });
        }}
      >
        Continue with GitHub
      </button>
    {/if}
  </div>

auth-client

import { createAuthClient } from "better-auth/svelte"
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: "http://localhost:4000"
})


It's quite hard to test or debug what is happening. I am wondering if there's an issue with better-sqlite3 but cannot test.

Originally created by @weepy on GitHub (May 2, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce I set up the basic better-auth sveltekit app. ### Current vs. Expected behavior After logging in, I can see the correct user etc in the database. But there session is null, whether I use getSession, useSession or else. ### What version of Better Auth are you using? lastest ### Provide environment information ```bash - Macos M1 - Node - Sveltekit latest - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import Database from "better-sqlite3"; import { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } from "$env/static/private"; import { BETTER_AUTH_URL, BETTER_AUTH_SECRET, SQLITE_DB_PATH } from "$env/static/private"; const SqliteDb = new Database(SQLITE_DB_PATH); export const auth = betterAuth({ baseURL: BETTER_AUTH_URL, secret: BETTER_AUTH_SECRET, database: SqliteDb, socialProviders: { github: { clientId: GITHUB_CLIENT_ID, clientSecret: GITHUB_CLIENT_SECRET, }, } }) ``` ### Additional context Hooks ------ import { auth } from "$lib/auth"; // path to your auth file import { svelteKitHandler } from "better-auth/svelte-kit"; export async function handle({ event, resolve }) { return svelteKitHandler({ event, resolve, auth }); } Page ------ <script lang="ts"> import { authClient } from "$lib/auth-client"; const session = authClient.useSession(); </script> <div> {#if $session.data} <div> <p> {$session?.data?.user.name} </p> <button on:click={async () => { await authClient.signOut(); }} > Sign Out </button> </div> {:else} <button on:click={async () => { await authClient.signIn.social({ provider: "github", }); }} > Continue with GitHub </button> {/if} </div> ----- auth-client import { createAuthClient } from "better-auth/svelte" export const authClient = createAuthClient({ /** The base URL of the server (optional if you're using the same domain) */ baseURL: "http://localhost:4000" }) --- It's quite hard to test or debug what is happening. I am wondering if there's an issue with better-sqlite3 but cannot test.
Author
Owner

@rururux commented on GitHub (May 13, 2025):

I tried reproducing the issue, but I couldn't reproduce it on my end.
I've created a sample repository here: https://github.com/rururux/better-auth-sveltekit-issue-test
Could you please provide a minimal reproduction repository where the issue occurs?

@rururux commented on GitHub (May 13, 2025): I tried reproducing the issue, but I couldn't reproduce it on my end. I've created a sample repository here: https://github.com/rururux/better-auth-sveltekit-issue-test Could you please provide a minimal reproduction repository where the issue occurs?
Author
Owner

@Aiurvie commented on GitHub (May 17, 2025):

I'm using the username plugin, Next.js, and the same thing happens.

@Aiurvie commented on GitHub (May 17, 2025): I'm using the username plugin, Next.js, and the same thing happens.
Author
Owner

@vanbenj commented on GitHub (Jun 12, 2025):

This may be related to this:
https://github.com/better-auth/better-auth/issues/600

I put together a working Svelte example here: https://github.com/vanbenj/better-auth-sveltekit5-example

@vanbenj commented on GitHub (Jun 12, 2025): This may be related to this: https://github.com/better-auth/better-auth/issues/600 I put together a working Svelte example here: https://github.com/vanbenj/better-auth-sveltekit5-example
Author
Owner

@Kinfe123 commented on GitHub (Aug 14, 2025):

I could not reproduce this on my end. Closing this now and also seems to be duplicated. will reopen if could get a repro for this.

@Kinfe123 commented on GitHub (Aug 14, 2025): I could not reproduce this on my end. Closing this now and also seems to be duplicated. will reopen if could get a repro for 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#1151