♻️ Add abstractions for tracking server-side events (#1143)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Luke Vella 2024-06-10 20:23:49 +01:00 committed by GitHub
parent 81d2f2c0bd
commit d43bc631f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 69 additions and 12 deletions

View file

@ -17,7 +17,7 @@ import EmailProvider from "next-auth/providers/email";
import GoogleProvider from "next-auth/providers/google";
import { Provider } from "next-auth/providers/index";
import { PostHogClient } from "@/app/posthog";
import { posthog } from "@/app/posthog";
import { absoluteUrl } from "@/utils/absolute-url";
import { CustomPrismaAdapter } from "@/utils/auth/custom-prisma-adapter";
import { mergeGuestsIntoUser } from "@/utils/auth/merge-user";
@ -176,7 +176,6 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
},
callbacks: {
async signIn({ user, email, account, profile }) {
const posthog = PostHogClient();
const distinctId = user.email ?? user.id;
// prevent sign in if email is not verified
if (
@ -191,7 +190,6 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
reason: "email not verified",
},
});
await posthog?.shutdownAsync();
return false;
}
// Make sure email is allowed
@ -224,6 +222,14 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
await mergeGuestsIntoUser(user.id, [session.user.id]);
}
posthog?.identify({
distinctId,
properties: {
name: user.name,
email: user.email,
},
});
posthog?.capture({
distinctId,
event: "login",
@ -231,7 +237,6 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
method: account?.provider,
},
});
await posthog?.shutdownAsync();
}
return true;