Plugin Interface
A plugin is an object that implements thePlugin interface:
packages/core/src/types.ts:145-149 for the complete interface definition.
Plugin Hooks
Lifecycle hooks allow you to execute custom logic at specific points in the authentication flow. All hooks are optional and async.packages/core/src/types.ts:151-158 for the complete interface.
Hook Execution Order
Hooks are executed in the order they’re defined in your plugin configuration. If you have multiple plugins, their hooks are called sequentially.Sign Up Flow
beforeSignUp- Before user account is created- User account created in database
- Account record created (credential/phone/OAuth)
afterSignUp- After user account is created- Session created
Sign In Flow
- Credentials validated
beforeSignIn- Before session is created- Session created
afterSignIn- After session is created
Available Hooks
beforeSignUp
beforeSignUp
Called before a new user account is created. Use this to validate or modify user data before it’s saved.Parameters:Used in:
data: Partial<User>- User data that will be used to create the account
- Email+password sign-up (
packages/core/src/routes/sign-up.ts:39-41) - Phone OTP verification (
packages/core/src/routes/otp-verify.ts:61-66) - OAuth callback (
packages/core/src/routes/oauth-callback.ts:69-71)
afterSignUp
afterSignUp
Called after a new user account is successfully created. Use this for post-registration tasks.Parameters:Used in:
user: User- The newly created user object
- Email+password sign-up (
packages/core/src/routes/sign-up.ts:70-72) - Phone OTP verification (
packages/core/src/routes/otp-verify.ts:84-86) - OAuth callback (
packages/core/src/routes/oauth-callback.ts:82-84)
beforeSignIn
beforeSignIn
Called before a session is created for an existing user. Use this to enforce access policies.Parameters:Used in:
user: User- The user attempting to sign in
- Email+password sign-in (
packages/core/src/routes/sign-in.ts:53-55) - OAuth callback (
packages/core/src/routes/oauth-callback.ts:105-107)
afterSignIn
afterSignIn
Called after a session is successfully created. Use this for audit logging and analytics.Parameters:Used in:
user: User- The authenticated usersession: Session- The newly created session
- Email+password sign-in (
packages/core/src/routes/sign-in.ts:59-61) - Phone OTP verification (
packages/core/src/routes/otp-verify.ts:91-93) - OAuth callback (
packages/core/src/routes/oauth-callback.ts:114-116)
beforeSignOut
beforeSignOut
Called before a session is deleted. Use this for cleanup tasks.Parameters:Note: This hook is defined in the interface but not yet implemented in the core routes. You can implement it when creating custom sign-out routes.
session: Session- The session being deleted
afterOTPVerified
afterOTPVerified
Called after an OTP is successfully verified. Use this for phone/email verification-specific logic.Parameters:Used in:
user: User- The user who verified the OTPtype: VerificationType- The type of OTP verified (‘phone-otp’ or ‘email-otp’)
- Phone/Email OTP verification (
packages/core/src/routes/otp-verify.ts:98-100)
Creating a Plugin
Here’s how to create a custom plugin:Registering Plugins
Add plugins to your auth configuration:Plugins are executed in the order they’re defined. If a plugin throws an error, the authentication flow is aborted and subsequent plugins won’t execute.
Custom Routes
Plugins can also add custom routes to the auth router:Example Plugins
- Audit Logging
- Email Verification
- Rate Limiting
Error Handling
When a plugin hook throws an error, the authentication flow is aborted:Best Practices
Keep Hooks Fast
Plugin hooks block the authentication flow. Keep them fast by:
- Using async operations efficiently
- Offloading heavy tasks to background jobs
- Caching frequently accessed data
Handle Errors Gracefully
Always handle errors in hooks:
- Use try-catch blocks
- Log errors for debugging
- Return user-friendly messages
- Don’t expose sensitive data
Test Thoroughly
Test your plugins:
- Unit test each hook
- Test error scenarios
- Test hook execution order
- Test with multiple plugins
Document Your Plugins
Make plugins maintainable:
- Document hook purposes
- List external dependencies
- Provide usage examples
- Version your plugins
Next Steps
Authentication Flows
Learn where plugin hooks are called in each flow
Database Adapters
Understand how to interact with the database in plugins