Skip to content

Commit fcaf619

Browse files
committed
add login / register posthog events
1 parent 7a85478 commit fcaf619

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

packages/auth/src/auth-options.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type DefaultSession, type NextAuthOptions } from "next-auth";
33
import GitHubProvider from "next-auth/providers/github";
44
import GoogleProvider from "next-auth/providers/google";
55

6+
import { posthog } from "@acme/api/posthog";
67
import { prisma } from "@acme/db";
78

89
/**
@@ -20,10 +21,13 @@ declare module "next-auth" {
2021
} & DefaultSession["user"];
2122
}
2223

23-
// interface User {
24-
// // ...other properties
25-
// // role: UserRole;
26-
// }
24+
interface Profile {
25+
sub?: string;
26+
name?: string;
27+
email?: string;
28+
image?: string;
29+
email_verified?: string;
30+
}
2731
}
2832

2933
/**
@@ -40,6 +44,45 @@ export const authOptions: NextAuthOptions = {
4044
}
4145
return session;
4246
},
47+
signIn({ account, profile }) {
48+
if (account?.provider === "google") {
49+
return !!profile?.email_verified;
50+
}
51+
return true; // Do different verification for other providers that don't have `email_verified`
52+
},
53+
},
54+
events: {
55+
signIn: ({ user, account, isNewUser }) => {
56+
posthog?.capture({
57+
distinctId: user.id,
58+
event: "signed in",
59+
properties: {
60+
name: user.name,
61+
email: user.email,
62+
provider: account?.provider,
63+
isNewUser,
64+
},
65+
});
66+
void posthog?.shutdownAsync();
67+
},
68+
signOut: ({ session }) => {
69+
// posthog?.capture({
70+
// distinctId: session.id,
71+
// event: "logged out",
72+
// });
73+
console.log("add logout event");
74+
},
75+
createUser: ({ user }) => {
76+
posthog?.capture({
77+
distinctId: user.id,
78+
event: "create user",
79+
properties: {
80+
name: user.name,
81+
email: user.email,
82+
},
83+
});
84+
void posthog?.shutdownAsync();
85+
},
4386
},
4487
adapter: PrismaAdapter(prisma),
4588
providers: [
@@ -63,7 +106,7 @@ export const authOptions: NextAuthOptions = {
63106
],
64107
pages: {
65108
signIn: "/sign-in",
66-
signOut: "/auth/signout",
67-
error: "/auth/error", // Error code passed in query string as ?error=
109+
// signOut: "/auth/signout",
110+
// error: "/auth/error", // Error code passed in query string as ?error=
68111
},
69112
};

turbo.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"NODE_ENV",
4545
"SKIP_ENV_VALIDATION",
4646
"VERCEL",
47-
"VERCEL_URL"
47+
"VERCEL_URL",
48+
"GOOGLE_CLIENT_ID",
49+
"GOOGLE_CLIENT_SECRET"
4850
]
4951
}

0 commit comments

Comments
 (0)