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;
...
}
}),
},
});
Solution:
Hi, maybe try after hook in database. I use it to send a wellcome mail to the user after he sign up. https://www.better-auth.com/docs/concepts/database#database-hooks...
Database | Better Auth
Learn how to use a database with Better Auth.
Jump to solution
6 Replies
Solution
Big Chunckus
Big Chunckus6d ago
Hi, maybe try after hook in database. I use it to send a wellcome mail to the user after he sign up. https://www.better-auth.com/docs/concepts/database#database-hooks
Database | Better Auth
Learn how to use a database with Better Auth.
Rustam
RustamOP6d ago
Oh I didn’t know it existed! That’s perfect thank you!
Rustam
RustamOP6d ago
Oh actually i realised that this hook also runs on the server side which is similar to the hook I mentioned in the post. Any way to know when a client has signed up or created a session on the client side? I can imagine it can be a query parameter to the callback url after email verification or something
KiNFiSH
KiNFiSH6d ago
you can use useSesssion() hook to check if the user is signed up and actually have a session with it. if i get your point through
Rustam
RustamOP6d ago
I ended up setting a query param on the callbackURL ( callbackURL: '/dashboard?isNewUser=true'). And then checking if isNewUser is set on the URL before calling identify. Posthog recommends to only call identify when the user signs up / logs in and doesn't recommend to do it on every request.

Did you find this page helpful?