Session Token Lost After Sign-In #1250

Closed
opened 2026-03-13 08:30:01 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @D0nte53 on GitHub (May 22, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Auth Server:

Email and Password Config :

emailAndPassword: {
  enabled: true,
  disableSignUp: false,
  requireEmailVerification: false,
  maxPasswordLength: 128,
  minPasswordLength: 8,
  sendResetPassword: async ({ user, url, token }: { user: User, url: string, token: string }, request?: Request) => {
    await sendPasswordResetEmail({ email: user.email, token, url });
  },
  resetPasswordTokenExpiresIn: hours.hourToSecond(1),
  password: {
    hash: _hashPassword,
    verify: (data: { hash: string; password: string }) => _comparePassword(data.password, data.hash),
  },
  autoSignIn: true,
  revokeSessionsOnPasswordReset: true,
},

Session Config:

session: {
  modelName: "Session",
  fields: {
    // ...
  },
  expiresIn: hours.dayToSecond(1),
  updateAge: hours.dayToSecond(1),
  disableSessionRefresh: false,
  cookieCache: {
    maxAge: hours.minuteToSecond(5),
    enabled: false,
  },
  freshAge: hours.dayToSecond(1),
  storeSessionInDatabase: true,
},

TRPC Context:

export const createTRPCContext = async (opts: FetchCreateContextFnOptions) => {
  const session = await auth.api.getSession({
    headers: opts.resHeaders,
  });

  return {
    session,
  };
};

Auth Client:

const authClient = createAuthClient({
  baseURL: process.env.SERVER_URL,
  plugins: [
    inferAdditionalFields<typeof auth>(),
    customSessionClient<typeof auth>(),
    magicLinkClient()
  ],
});

App

SignIn :

await authClient.signIn.email({
  email: formData.email,
  password: formData.password,
  rememberMe: false,
  ...(options ?? {}),
}, fetchOptions);

Current vs. Expected behavior

Current Behavior :
After signing in, the better-auth session_token cookie is initially set, but it disappears upon navigating to another page.
Note: If I don’t use a callbackUrl, the session_token cookie stays, but it still gets lost as soon as I navigate again.

Expected Behavior :
After signing in, the session_token cookie should persist across navigations, maintaining the user’s authenticated session. The user should remain logged in without needing to re-authenticate after page transitions.

What version of Better Auth are you using?

1.2.8

Provide environment information

**Versions:**

* next: `15.4.0-canary.38`
* better-auth: `^1.2.8`
* prisma: `^6.8.2`
* @trpc/server: `^11.0.0-rc.682`
* @tanstack/react-query: `^5.62.15`

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

Client

Auth config (if applicable)


Additional context

No response

Originally created by @D0nte53 on GitHub (May 22, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce **Auth Server:** Email and Password Config : ```ts emailAndPassword: { enabled: true, disableSignUp: false, requireEmailVerification: false, maxPasswordLength: 128, minPasswordLength: 8, sendResetPassword: async ({ user, url, token }: { user: User, url: string, token: string }, request?: Request) => { await sendPasswordResetEmail({ email: user.email, token, url }); }, resetPasswordTokenExpiresIn: hours.hourToSecond(1), password: { hash: _hashPassword, verify: (data: { hash: string; password: string }) => _comparePassword(data.password, data.hash), }, autoSignIn: true, revokeSessionsOnPasswordReset: true, }, ``` Session Config: ```ts session: { modelName: "Session", fields: { // ... }, expiresIn: hours.dayToSecond(1), updateAge: hours.dayToSecond(1), disableSessionRefresh: false, cookieCache: { maxAge: hours.minuteToSecond(5), enabled: false, }, freshAge: hours.dayToSecond(1), storeSessionInDatabase: true, }, ``` --- **TRPC Context:** ```ts export const createTRPCContext = async (opts: FetchCreateContextFnOptions) => { const session = await auth.api.getSession({ headers: opts.resHeaders, }); return { session, }; }; ``` --- **Auth Client:** ```ts const authClient = createAuthClient({ baseURL: process.env.SERVER_URL, plugins: [ inferAdditionalFields<typeof auth>(), customSessionClient<typeof auth>(), magicLinkClient() ], }); ``` --- **App** SignIn : ```ts await authClient.signIn.email({ email: formData.email, password: formData.password, rememberMe: false, ...(options ?? {}), }, fetchOptions); ``` ### Current vs. Expected behavior Current Behavior : After signing in, the better-auth session_token cookie is initially set, but it disappears upon navigating to another page. Note: If I don’t use a callbackUrl, the session_token cookie stays, but it still gets lost as soon as I navigate again. Expected Behavior : After signing in, the session_token cookie should persist across navigations, maintaining the user’s authenticated session. The user should remain logged in without needing to re-authenticate after page transitions. ### What version of Better Auth are you using? 1.2.8 ### Provide environment information ```bash **Versions:** * next: `15.4.0-canary.38` * better-auth: `^1.2.8` * prisma: `^6.8.2` * @trpc/server: `^11.0.0-rc.682` * @tanstack/react-query: `^5.62.15` ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
Author
Owner

@Qodestackr commented on GitHub (May 22, 2025):

    const hdrs = await headers();
    const session = await auth.api.getSession({
      headers: hdrs,
    });

This always returns null mahn, endpoints in server side dont work nicely

@Qodestackr commented on GitHub (May 22, 2025): ```ts const hdrs = await headers(); const session = await auth.api.getSession({ headers: hdrs, }); ``` This always returns null mahn, endpoints in server side dont work nicely
Author
Owner

@D0nte53 commented on GitHub (May 22, 2025):

const hdrs = await headers();
const session = await auth.api.getSession({
headers: hdrs,
});
This always returns null mahn, endpoints in server side dont work nicely

I had already tried using this syntax — if you're sure it's correct, I'll keep it. However, the issue remains the same. Thanks for your input!

@D0nte53 commented on GitHub (May 22, 2025): > const hdrs = await headers(); > const session = await auth.api.getSession({ > headers: hdrs, > }); > This always returns null mahn, endpoints in server side dont work nicely I had already tried using this syntax — if you're sure it's correct, I'll keep it. However, the issue remains the same. Thanks for your input!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1250