Consistent Signup Event Tracking Across Auth Methods
I’m trying to track signup events across all auth methods (email/password, magic link, OAuth) using Better Auth hooks. Email signups work fine, but I can’t access user data after OAuth callbacks.
Current Implementation
export const auth = betterAuth({
hooks: {
after: createAuthMiddleware(async (ctx) => {
const isSignup =
ctx.path === "/signup" ctx.path.startsWith("/callback/");
if (isSignup && ctx.body?.status === 200) {
const { user } = ctx.body {};
if (user) await trackSignupEvent(user);
}
}),
},
});
What Works ✅
- Email/Password
- Path: /signup
- ctx.body.user is available → tracking works
What Fails ❌
- OAuth (e.g., /callback/google)
Questions
1. How can I reliably access user data during OAuth callbacks? Different hook? Different context properties?
2. Are there separate hooks for different auth flows?
3. What’s the recommended server-side pattern for consistent signup tracking across methods?
4. For OAuth: what properties are exposed in the context, and how do I get the user record?
Current Workaround
Handling OAuth tracking client-side in the onboarding component, but this is unreliable and misses edge cases...
Environment
Better Auth (latest)
Next.js App Router
PostgreSQL + Prisma
Auth methods: email/password, magic link, Google OAuth
1 Reply
Before account create database hook maybe? Or before user