Event Hooks
Configure event hooks to trigger custom actions on specific authentication events.
Event hooks in GoBetterAuth allow you to trigger custom actions when specific authentication-related events occur. This enables you to integrate with external systems, send notifications, or perform additional logic in response to user activity.
Available Event Hooks
You can configure the following event hooks:
import (
gobetterauthmodels "github.com/GoBetterAuth/go-better-auth/models"
)
type EventHooksConfig struct {
OnUserSignedUp func(user gobetterauthmodels.User) error
OnUserLoggedIn func(user gobetterauthmodels.User) error
OnEmailVerified func(user gobetterauthmodels.User) error
OnPasswordChanged func(user gobetterauthmodels.User) error
OnEmailChanged func(user gobetterauthmodels.User) error
}- OnUserSignedUp: Triggered when a new user signs up.
- OnUserLoggedIn: Triggered when a user successfully logs in.
- OnEmailVerified: Triggered when a user's email is verified.
- OnPasswordChanged: Triggered when a user changes their password.
- OnEmailChanged: Triggered when a user changes their email address.
How to Use Event Hooks
To use event hooks, provide your own functions for the events you want to handle. These functions receive the relevant User object and can perform any custom logic you need.
Examples
Below are some event hook examples that reflect real-world workflows. These patterns emphasise non-blocking behaviour, idempotency, and security considerations.
import (
gobetterauthconfig "github.com/GoBetterAuth/go-better-auth/config"
gobetterauthmodels "github.com/GoBetterAuth/go-better-auth/models"
)
func doSomething(user gobetterauthmodels.User) error {
// Do something here...
return nil
}
config := gobetterauthconfig.NewConfig(
gobetterauthconfig.WithEventHooks(gobetterauthmodels.EventHooksConfig{
OnUserSignedUp: func (user gobetterauthmodels.User) error {
// You can perform more actions here...
return doSomething(user)
},
}),
)Summary
Event hooks execute asynchronously in goroutines, preventing any disruption to the core authentication process. They offer a flexible way to extend GoBetterAuth by responding to key authentication events. By defining custom functions, you can adapt the authentication experience to your application's specific needs, integrating seamlessly with analytics, notifications, logging, or additional business logic.
