Rustam
Rustam
BABetter Auth
Created by Rustam on 5/9/2025 in #help
Post sign up hook for google provider
Hi, I've implemented a post sign up hook for an email provider where I send an internal event to my team when a user signs up. Now I would like to do the same for the google provider. Can you advice how I can identify when the user signs up (creates a new account) as opposed to logs in? Here's my existing after hook
hooks: {
after: createAuthMiddleware(async (ctx) => {
const isFromEmail = ctx.path.startsWith('/verify-email');

if (isFromEmail) {
const newSession = ctx.context.newSession;
if (newSession) {
const user = newSession.user;
try {
await sendProductEventMessage(`[NEW USER SIGNED UP] ${user.name} (${user.email})`);
} catch (error) {
console.error('Error sending product event message', error);
}
}
}
}),
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
const isFromEmail = ctx.path.startsWith('/verify-email');

if (isFromEmail) {
const newSession = ctx.context.newSession;
if (newSession) {
const user = newSession.user;
try {
await sendProductEventMessage(`[NEW USER SIGNED UP] ${user.name} (${user.email})`);
} catch (error) {
console.error('Error sending product event message', error);
}
}
}
}),
},
3 replies
BABetter Auth
Created by Rustam on 4/14/2025 in #help
Posthog identify after sign up
Hi all, I'm using posthog for analytics in my next js app and planning to make use of the identify feature (https://posthog.com/docs/product-analytics/identify). I use the email and password flow with email verification from better auth. It's pretty easy to set it up when a user logs in as I can use the "onSuccess" callback. However, I can't wrap my head around how to do it after a successful sign up. I know you have a hooks.after callback but it runs on the server side and posthog needs to call identify on the client side. Could you suggest what the best way to do it? Thank you? Here's my auth.ts
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
session: {
expiresIn: ONE_YEAR_IN_SECONDS,
},
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
const email = getResetPasswordEmail(url);
await sendEmail(user.email, 'Reset your password', email);
},
},
emailVerification: {
sendVerificationEmail: async ({ user, url }) => {
const email = getSignUpVerificationEmail(url);
await sendEmail(user.email, 'Verify your email address', email);
},
expiresAt: ONE_DAY_IN_SECONDS,
sendOnSignUp: true,
autoSignInAfterVerification: true,
},
plugins: [
customSession(async ({ user, session }) => {
const roles = await getRoles();
return {
session,
user: {
...user,
roles: roles.map((role: { name: string }) => role.name),
},
};
}),
],
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith('/verify-email')) {
const newSession = ctx.context.newSession;
...
}
}),
},
});
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
session: {
expiresIn: ONE_YEAR_IN_SECONDS,
},
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
const email = getResetPasswordEmail(url);
await sendEmail(user.email, 'Reset your password', email);
},
},
emailVerification: {
sendVerificationEmail: async ({ user, url }) => {
const email = getSignUpVerificationEmail(url);
await sendEmail(user.email, 'Verify your email address', email);
},
expiresAt: ONE_DAY_IN_SECONDS,
sendOnSignUp: true,
autoSignInAfterVerification: true,
},
plugins: [
customSession(async ({ user, session }) => {
const roles = await getRoles();
return {
session,
user: {
...user,
roles: roles.map((role: { name: string }) => role.name),
},
};
}),
],
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith('/verify-email')) {
const newSession = ctx.context.newSession;
...
}
}),
},
});
9 replies