Skip to content

Commit

Permalink
add login / register posthog events
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconLP committed May 13, 2023
1 parent 7a85478 commit fcaf619
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
55 changes: 49 additions & 6 deletions packages/auth/src/auth-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type DefaultSession, type NextAuthOptions } from "next-auth";
import GitHubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";

import { posthog } from "@acme/api/posthog";
import { prisma } from "@acme/db";

/**
Expand All @@ -20,10 +21,13 @@ declare module "next-auth" {
} & DefaultSession["user"];
}

// interface User {
// // ...other properties
// // role: UserRole;
// }
interface Profile {
sub?: string;
name?: string;
email?: string;
image?: string;
email_verified?: string;
}
}

/**
Expand All @@ -40,6 +44,45 @@ export const authOptions: NextAuthOptions = {
}
return session;
},
signIn({ account, profile }) {
if (account?.provider === "google") {
return !!profile?.email_verified;
}
return true; // Do different verification for other providers that don't have `email_verified`
},
},
events: {
signIn: ({ user, account, isNewUser }) => {
posthog?.capture({
distinctId: user.id,
event: "signed in",
properties: {
name: user.name,
email: user.email,
provider: account?.provider,
isNewUser,
},
});
void posthog?.shutdownAsync();
},
signOut: ({ session }) => {
// posthog?.capture({
// distinctId: session.id,
// event: "logged out",
// });
console.log("add logout event");
},
createUser: ({ user }) => {
posthog?.capture({
distinctId: user.id,
event: "create user",
properties: {
name: user.name,
email: user.email,
},
});
void posthog?.shutdownAsync();
},
},
adapter: PrismaAdapter(prisma),
providers: [
Expand All @@ -63,7 +106,7 @@ export const authOptions: NextAuthOptions = {
],
pages: {
signIn: "/sign-in",
signOut: "/auth/signout",
error: "/auth/error", // Error code passed in query string as ?error=
// signOut: "/auth/signout",
// error: "/auth/error", // Error code passed in query string as ?error=
},
};
4 changes: 3 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"NODE_ENV",
"SKIP_ENV_VALIDATION",
"VERCEL",
"VERCEL_URL"
"VERCEL_URL",
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET"
]
}

0 comments on commit fcaf619

Please sign in to comment.