[GH-ISSUE #860] Session Cookie #8470

Closed
opened 2026-04-13 03:33:01 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @Lanmi2304 on GitHub (Dec 11, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/860

Describe the bug
I am having difficulty creating session cookies within my plugin. The cookie is only set on the second API route call. Although the session is successfully created, its token is always delayed for that initial API request.

I also attempted to use await ctx.setSignedCookie, but the behavior remained the same.

  async (ctx) => {
        try {
          const session = await ctx.context.internalAdapter.createSession(
            'qWF5gPK6VJI30Aqjj5ET1',
            undefined,
          );

          await setSessionCookie(ctx, { session, user });
          return { message: 'Cookie has been successfully set!' };
        } catch (error) {
          console.error('Error handling user session:', error);
          throw new Error('Unable to process user session. Please try again.');
        }

Expected behavior
The goal is to ensure that when I initially call the API route, the session cookie is set immediately. As mentioned above, the session token is always delayed for that initial API request.

Originally created by @Lanmi2304 on GitHub (Dec 11, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/860 **Describe the bug** I am having difficulty creating session cookies within my plugin. The cookie is only set on the second API route call. Although the session is successfully created, its token is always delayed for that initial API request. I also attempted to use `await ctx.setSignedCookie`, but the behavior remained the same. ``` async (ctx) => { try { const session = await ctx.context.internalAdapter.createSession( 'qWF5gPK6VJI30Aqjj5ET1', undefined, ); await setSessionCookie(ctx, { session, user }); return { message: 'Cookie has been successfully set!' }; } catch (error) { console.error('Error handling user session:', error); throw new Error('Unable to process user session. Please try again.'); } ``` **Expected behavior** The goal is to ensure that when I initially call the API route, the session cookie is set immediately. As mentioned above, the session token is always delayed for that initial API request.
GiteaMirror added the locked label 2026-04-13 03:33:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8470