Skip to content

Conversation

@maetad
Copy link

@maetad maetad commented Oct 3, 2025

Fix: Make BeforeHook/AfterHook decorators work

Fixes #42

Problem

@BeforeHook and @AfterHook were not firing.

Root Cause

A shallow copy of the hooks object in onModuleInit() was overwriting the hooks configuration, preventing registration.

Solution

Removed the problematic shallow copy and the redundant hooks check in setupHooks().

Changes

  • Removed this.options.auth.options.hooks = { ...this.options.auth.options.hooks } from onModuleInit()
  • Removed redundant if (!this.options.auth.options.hooks) return; check in setupHooks()
  • Added e2e tests to verify hook behavior

Testing

  • Added tests for @BeforeHook and @AfterHook
  • Verified hooks fire on auth endpoints
  • All tests pass

This resolves the issue reported in #42 and enables hook decorators to work as intended.

@ThallesP
Copy link
Owner

ThallesP commented Oct 4, 2025

Fix: Make BeforeHook/AfterHook decorators work

Fixes #42

Problem

@BeforeHook and @AfterHook were not firing.

Root Cause

A shallow copy of the hooks object in onModuleInit() was overwriting the hooks configuration, preventing registration.

Solution

Removed the problematic shallow copy and the redundant hooks check in setupHooks().

Changes

  • Removed this.options.auth.options.hooks = { ...this.options.auth.options.hooks } from onModuleInit()
  • Removed redundant if (!this.options.auth.options.hooks) return; check in setupHooks()
  • Added e2e tests to verify hook behavior

Testing

  • Added tests for @BeforeHook and @AfterHook
  • Verified hooks fire on auth endpoints
  • All tests pass

This resolves the issue reported in #42 and enables hook decorators to work as intended.

That'll solve the issue if you're passing an empty hooks object to better-auth (which is exactly what I did on #43), but I'm also trying to find a way where it doesn't require doing that like before. I'm still debugging what's happening.

In the meantime, we can proceed with requiring the hooks object as long as it's documented in the readme.

@ThallesP
Copy link
Owner

ThallesP commented Oct 4, 2025

fixed under #43

@ThallesP ThallesP closed this Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Hook decorators (@BeforeHook, @AfterHook) not working

2 participants