injectLastSeenAtHooks
Injects a plugin into Better Auth that tracks the last seen timestamp for users. This utility automatically updates alastSeenAt column in your user table whenever a user signs in, signs up, or their session is retrieved.
Signature
Parameters
The Better Auth instance to inject hooks into
Configuration object for last seen tracking
Behavior
- Creates a Better Auth plugin that adds a
lastSeenAtfield to the user schema - Automatically updates the timestamp on:
- User sign-up
- User sign-in (email/password or OAuth)
- Session retrieval (get-session)
- Uses the database adapter directly for updates
- Merges the field into
auth.options.user.additionalFieldsso it’s returned with user data
Usage Example
Database Setup
Before enabling this feature, ensure your user table has the corresponding column:Notes
- The function is idempotent - calling it multiple times won’t inject duplicate plugins
- The plugin ID is
better-auth-studio-last-seen - Updates are performed asynchronously and won’t block authentication flows
- Failed updates are silently ignored to prevent disrupting authentication
injectEventHooks
Injects event tracking hooks into Better Auth that automatically capture authentication events (sign-ups, logins, OAuth flows, organization operations, etc.) and send them to your configured event ingestion provider.Signature
Parameters
The Better Auth instance to inject event hooks into
Event ingestion configuration object
Tracked Events
The injected hooks automatically track:- User events:
user.joined,user.logged_in,user.logged_out,user.updated,user.banned,user.unbanned - Session events:
session.created - OAuth events:
oauth.sign_in,oauth.linked,oauth.unlinked - Organization events:
organization.created,organization.updated,organization.deleted - Member events:
member.added,member.removed,member.role_changed - Team events:
team.created,team.updated,team.deleted,team.member.added,team.member.removed - Invitation events:
invitation.created,invitation.accepted,invitation.rejected,invitation.cancelled - Phone number events:
phone_number.otp_requested,phone_number.verification
Usage Example
Notes
- The function is idempotent - calling it multiple times won’t inject duplicate plugins
- The plugin ID is
better-auth-studio-events - Events are captured asynchronously and won’t block authentication flows
- Also wraps organization plugin hooks and auth callbacks for comprehensive tracking