Both hooks and plugins[].hooks only get called with /get-session path #1196

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

Originally created by @jrgtt on GitHub (May 12, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

hooks and plugins only get called when the context path is /get-session.

Image

Current vs. Expected behavior

Expected to see other endpoints triggering both hooks and plugins such as /sign-in and /sign-up.

What version of Better Auth are you using?

1.2.7

Provide environment information

- OS: MacOS Sequoia 15.0 (24A335)

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

Backend, Client

Auth config (if applicable)

import { betterAuth } from "better-auth"

export const auth = betterAuth({
  emailAndPassword: {
    enabled: true,
    autoSignIn: true,
  },
  database: drizzleAdapter(db, {
    provider: 'pg'
  }),
  hooks: {
    after: createAuthMiddleware(async (ctx) => {
      console.log('hooks > after > ctx path: %s', ctx.path);
    }),
  },
  plugins: [
    nextCookies(),
    {
      hooks: {
        after: [
          {
            matcher(ctx) {
              console.log('matcher context path: %s', ctx.path);
              return true;
            },

            handler: createAuthMiddleware(async (ctx) => {
              console.log('handler context path: %s', ctx.path);
            }),
          },
        ],
      },
    },
    anonymous({
      emailDomainName: 'guest.user',
      onLinkAccount: async ({ anonymousUser, newUser }) => {
        console.log(
          'Linking %s and %s',
          anonymousUser.user.id,
          newUser.user.id,
        );
      },
    }),
  ],
});

Additional context

After I've added the anonymous plugin, I've noticed that linking accounts was not being correctly done as other users have pointed out here.

When I inspected the plugin itself, I've noticed that the function should have been called but it didn't.

So, I've tried to reproduce with two approaches one with hooks and other with plugins. When I've logged the value of context.path the value was always /get-session and never anything else.

Originally created by @jrgtt on GitHub (May 12, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce `hooks` and `plugins` only get called when the context path is `/get-session`. ![Image](https://github.com/user-attachments/assets/06b77553-7f3c-4f50-a7aa-80199339df35) ### Current vs. Expected behavior Expected to see other endpoints triggering both `hooks` and `plugins` such as `/sign-in` and `/sign-up`. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - OS: MacOS Sequoia 15.0 (24A335) ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true, autoSignIn: true, }, database: drizzleAdapter(db, { provider: 'pg' }), hooks: { after: createAuthMiddleware(async (ctx) => { console.log('hooks > after > ctx path: %s', ctx.path); }), }, plugins: [ nextCookies(), { hooks: { after: [ { matcher(ctx) { console.log('matcher context path: %s', ctx.path); return true; }, handler: createAuthMiddleware(async (ctx) => { console.log('handler context path: %s', ctx.path); }), }, ], }, }, anonymous({ emailDomainName: 'guest.user', onLinkAccount: async ({ anonymousUser, newUser }) => { console.log( 'Linking %s and %s', anonymousUser.user.id, newUser.user.id, ); }, }), ], }); ``` ### Additional context After I've added the [anonymous](https://www.better-auth.com/docs/plugins/anonymous) plugin, I've noticed that [linking accounts](https://www.better-auth.com/docs/plugins/anonymous#link-account) was not being correctly done as other users have pointed out [here](https://github.com/better-auth/better-auth/issues/1457). When I inspected the plugin itself, I've noticed that the function [should have been called](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/anonymous/index.ts#L227) but it didn't. So, I've tried to reproduce with two approaches one with [hooks](https://www.better-auth.com/docs/concepts/hooks) and other with [plugins](https://www.better-auth.com/docs/concepts/plugins). When I've logged the value of `context.path` the value was always `/get-session` and never anything else.
Author
Owner

@Kinfe123 commented on GitHub (Jun 8, 2025):

I couldn’t reproduce the issue on my end. Can you try updating to the latest version and share a repro if it still happens?

@Kinfe123 commented on GitHub (Jun 8, 2025): I couldn’t reproduce the issue on my end. Can you try updating to the latest version and share a repro if it still happens?
Author
Owner

@ping-maxwell commented on GitHub (Jul 8, 2025):

Hello @jrgtt it's been a while now, are you still facing this issue on latest?
I'll close this for now - if you're still experiencing this LMK and I can happily re-open this!

@ping-maxwell commented on GitHub (Jul 8, 2025): Hello @jrgtt it's been a while now, are you still facing this issue on latest? I'll close this for now - if you're still experiencing this LMK and I can happily re-open 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#1196