[GH-ISSUE #782] Incomplete session Object in databaseHooks.session.update.before Hook Prevents custom session updates #25753

Closed
opened 2026-04-17 16:01:34 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @romainchkr on GitHub (Dec 6, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/782

Describe the bug

When the databaseHooks.session.update.before hook is called by Better Auth, the session parameter only contains the expiresAt property.
However, the following hooks work perfectly (the session object as parameter has all its properties set):

  • databaseHooks.session.update.after
  • databaseHooks.session.create.before

Using databaseHooks.session.create.before and databaseHooks.session.update.before would allow me to add/update custom properties on the sessionobject like jwt token


Code Snippet

import "server-only";

...

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    ...
  }),
  emailAndPassword: {
   ...
  },
  plugins: [
    ...
  },
  advanced: {
    ...
  },
  session: {
    modelName: "session",
    additionalFields: {
      supabaseJwt: {
        type: "string",
        required: false,
      },
    },
    cookieCache: {
      enabled: true,
      maxAge: 5 * 60, // Cache duration in seconds
    },
  },
  databaseHooks: {
    session: {
      create: {
        // Works as expected
        before: async (session) => {
          console.log("session create", session);
          const supabaseJwt = ...
          return {
            data: { ...session, supabaseJwt: supabaseJwt },
          };
        },
      },
      update: {
        // Bug : session object only contains expiresAt
        before: async (session) => {
          console.log("session update before", session, session.userId);
          // Issue: `session` only contains { expiresAt: 2024-12-13T08:25:28.544Z }
          // Question: Is there a way to retrieve the whole session to update the JWT? 
          // what i want to do :
          // const supabaseJwt = ...
          // return { data: { ...session, supabaseJwt: supabaseJwt } };
        },
        after: async (session) => {
          console.log("session update after", session); // Retrieves the full session object
        },
      },
    },
  },
  baseURL: process.env.BETTER_AUTH_URL,
});

To Reproduce
Steps to reproduce the behavior:

  1. Set a basic auth using better-auth
  2. Connect as a user
  3. Leave the website
  4. Come back after a few hours/days in order for the session to get updated when relaunching the website

Expected behavior
The databaseHooks.session.update.before hook should receive the complete session object (including properties like userId) so that the supabaseJwt token can be updated dynamically.

Additional context

  • I am using Supabase and Next.js.
  • The databaseHooks.session.create.before works perfectly for adding the supabaseJwt token to the session object and making it available in the frontend.
  • However, during session updates, it is currently impossible to update the supabaseJwt due to the incomplete session data in the databaseHooks.session.update.before hook.
Originally created by @romainchkr on GitHub (Dec 6, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/782 **Describe the bug** When the `databaseHooks.session.update.before` hook is called by **Better Auth**, the `session` parameter only contains the `expiresAt` property. However, the following hooks work perfectly (the session object as parameter has all its properties set): - `databaseHooks.session.update.after` - `databaseHooks.session.create.before` Using `databaseHooks.session.create.before` and `databaseHooks.session.update.before` would allow me to add/update custom properties on the `session`object like jwt token --- ### **Code Snippet** ```typescript import "server-only"; ... export const auth = betterAuth({ database: drizzleAdapter(db, { ... }), emailAndPassword: { ... }, plugins: [ ... }, advanced: { ... }, session: { modelName: "session", additionalFields: { supabaseJwt: { type: "string", required: false, }, }, cookieCache: { enabled: true, maxAge: 5 * 60, // Cache duration in seconds }, }, databaseHooks: { session: { create: { // Works as expected before: async (session) => { console.log("session create", session); const supabaseJwt = ... return { data: { ...session, supabaseJwt: supabaseJwt }, }; }, }, update: { // Bug : session object only contains expiresAt before: async (session) => { console.log("session update before", session, session.userId); // Issue: `session` only contains { expiresAt: 2024-12-13T08:25:28.544Z } // Question: Is there a way to retrieve the whole session to update the JWT? // what i want to do : // const supabaseJwt = ... // return { data: { ...session, supabaseJwt: supabaseJwt } }; }, after: async (session) => { console.log("session update after", session); // Retrieves the full session object }, }, }, }, baseURL: process.env.BETTER_AUTH_URL, }); ``` --- **To Reproduce** Steps to reproduce the behavior: 1. Set a basic auth using better-auth 2. Connect as a user 3. Leave the website 4. Come back after a few hours/days in order for the session to get updated when relaunching the website **Expected behavior** The `databaseHooks.session.update.before` hook should receive the **complete session object** (including properties like userId) so that the supabaseJwt token can be updated dynamically. **Additional context** - I am using Supabase and Next.js. - The `databaseHooks.session.create.before` works perfectly for adding the supabaseJwt token to the session object and making it available in the frontend. - However, during session updates, it is currently impossible to update the supabaseJwt due to the incomplete session data in the `databaseHooks.session.update.before hook`.
GiteaMirror added the locked label 2026-04-17 16:01:34 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jun 12, 2025):

Hi, @romainchkr. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary

  • The issue involves a limitation in the databaseHooks.session.update.before hook.
  • Currently, the session parameter is restricted to the expiresAt property.
  • This restriction prevents updating custom session properties, such as a JWT token.
  • You are seeking a solution to access the full session object in the update.before hook.
  • There have been no comments or developments on this issue yet.

Next Steps

  • Please let me know if this issue is still relevant to the latest version of the better-auth repository by commenting on this issue.
  • If there is no further activity, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

<!-- gh-comment-id:2967416355 --> @dosubot[bot] commented on GitHub (Jun 12, 2025): Hi, @romainchkr. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary** - The issue involves a limitation in the `databaseHooks.session.update.before` hook. - Currently, the `session` parameter is restricted to the `expiresAt` property. - This restriction prevents updating custom session properties, such as a JWT token. - You are seeking a solution to access the full session object in the `update.before` hook. - There have been no comments or developments on this issue yet. **Next Steps** - Please let me know if this issue is still relevant to the latest version of the better-auth repository by commenting on this issue. - If there is no further activity, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25753