[GH-ISSUE #8789] Before hook cookies not propagated and typo issues in docs #19823

Closed
opened 2026-04-15 19:10:35 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @dwalker93 on GitHub (Mar 26, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8789

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Create Simple better-auth app with try to set cookies in before hooks

Current vs. Expected behavior

Custom cookies set via ctx.setCookie() in before hooks don't appear in the HTTP response.
The same code works correctly in after hooks.

What version of Better Auth are you using?

1.5.6

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 11 Home Single Language",
    "release": "10.0.26200",
    "cpuCount": 12,
    "cpuModel": "Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz",
    "totalMemory": "23.88 GB",
    "freeMemory": "12.98 GB"
  },
  "node": {
    "version": "v24.14.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.9.0"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "16.2.1"
    },
    {
      "name": "react",
      "version": "19.2.4"
    }
  ],
  "databases": [
    {
      "name": "drizzle",
      "version": "^0.45.1"
    }
  ],
  "betterAuth": {
    "version": "^1.5.6",
    "config": {
      "emailAndPassword": {
        "enabled": true
      },
      "secondaryStorage": {},
      "hooks": {},
      "plugins": [
        {
          "name": "next-cookies",
          "config": {
            "id": "next-cookies",
            "hooks": {
              "before": [
                {}
              ],
              "after": [
                {}
              ]
            }
          }
        }
      ]
    }
  }
}

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

Documentation, Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { drizzle } from "drizzle-orm/node-postgres";
import * as schema from "./auth-schema";
import { redisSecondaryStorage } from "./redis-storage";
import { createAuthMiddleware } from "better-auth/api";
import { nextCookies } from "better-auth/next-js";

if (!process.env.NEON_DB_URL) {
  throw new Error("NEON_DB_URL is not defined");
}

const db = drizzle(process.env.NEON_DB_URL, { schema });

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg", schema }),
  emailAndPassword: { enabled: true },
  secondaryStorage: redisSecondaryStorage,
  hooks: {
    after: createAuthMiddleware(async (ctx) => {
      if (ctx.path === "/sign-in/email") {
        console.log("After auth middleware");
        ctx.setCookie("my-cookie", "value");
        await ctx.setSignedCookie(
          "my-signed-cookie",
          "value",
          ctx.context.secret,
          {
            maxAge: 1000,
          },
        );
        const cookie = ctx.getCookie("my-cookie");
        const signedCookie = await ctx.getSignedCookie(
          "my-signed-cookie",
          ctx.context.secret,
        );
        console.log(cookie, signedCookie);
      }
    }),
  },
  plugins: [nextCookies()],
});

Additional context

Found several typos in official documentation

Page: https://better-auth.com/docs/concepts/hooks#cookies

ctx.setCookies() → should be ctx.setCookie() (singular)
ctx.getCookies() → should be ctx.getCookie() (singular)
ctx.getSignedCookie("name") → should be ctx.getSignedCookie("name", secret) (missing secret param)

Originally created by @dwalker93 on GitHub (Mar 26, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8789 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Create Simple better-auth app with try to set cookies in before hooks ### Current vs. Expected behavior Custom cookies set via `ctx.setCookie()` in `before` hooks don't appear in the HTTP response. The same code works correctly in `after` hooks. ### What version of Better Auth are you using? 1.5.6 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 11 Home Single Language", "release": "10.0.26200", "cpuCount": 12, "cpuModel": "Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz", "totalMemory": "23.88 GB", "freeMemory": "12.98 GB" }, "node": { "version": "v24.14.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.9.0" }, "frameworks": [ { "name": "next", "version": "16.2.1" }, { "name": "react", "version": "19.2.4" } ], "databases": [ { "name": "drizzle", "version": "^0.45.1" } ], "betterAuth": { "version": "^1.5.6", "config": { "emailAndPassword": { "enabled": true }, "secondaryStorage": {}, "hooks": {}, "plugins": [ { "name": "next-cookies", "config": { "id": "next-cookies", "hooks": { "before": [ {} ], "after": [ {} ] } } } ] } } } ``` ### Which area(s) are affected? (Select all that apply) Documentation, Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { drizzle } from "drizzle-orm/node-postgres"; import * as schema from "./auth-schema"; import { redisSecondaryStorage } from "./redis-storage"; import { createAuthMiddleware } from "better-auth/api"; import { nextCookies } from "better-auth/next-js"; if (!process.env.NEON_DB_URL) { throw new Error("NEON_DB_URL is not defined"); } const db = drizzle(process.env.NEON_DB_URL, { schema }); export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", schema }), emailAndPassword: { enabled: true }, secondaryStorage: redisSecondaryStorage, hooks: { after: createAuthMiddleware(async (ctx) => { if (ctx.path === "/sign-in/email") { console.log("After auth middleware"); ctx.setCookie("my-cookie", "value"); await ctx.setSignedCookie( "my-signed-cookie", "value", ctx.context.secret, { maxAge: 1000, }, ); const cookie = ctx.getCookie("my-cookie"); const signedCookie = await ctx.getSignedCookie( "my-signed-cookie", ctx.context.secret, ); console.log(cookie, signedCookie); } }), }, plugins: [nextCookies()], }); ``` ### Additional context Found several typos in official documentation Page: https://better-auth.com/docs/concepts/hooks#cookies ctx.setCookies() → should be ctx.setCookie() (singular) ctx.getCookies() → should be ctx.getCookie() (singular) ctx.getSignedCookie("name") → should be ctx.getSignedCookie("name", secret) (missing secret param)
GiteaMirror added the platformdocslockedcorebug labels 2026-04-15 19:10:35 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Apr 5, 2026):

This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.

<!-- gh-comment-id:4187978027 --> @github-actions[bot] commented on GitHub (Apr 5, 2026): This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19823