add sign-in and sign-out events

This commit is contained in:
MarconLP 2023-04-23 12:52:31 +02:00
parent c3dad32a2e
commit 9ad8bcf507
No known key found for this signature in database
GPG key ID: A08A9C8B623F5EA5
3 changed files with 47 additions and 8 deletions

14
package-lock.json generated
View file

@ -34,7 +34,7 @@
"micro": "^10.0.1",
"micro-cors": "^0.1.1",
"next": "^13.3.0",
"next-auth": "^4.21.0",
"next-auth": "^4.22.1",
"posthog-js": "^1.53.4",
"posthog-node": "^3.1.0",
"react": "18.2.0",
@ -5350,9 +5350,9 @@
}
},
"node_modules/next-auth": {
"version": "4.22.0",
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.0.tgz",
"integrity": "sha512-08+kjnDoE7aQ52O996x6cwA3ffc2CbHIkrCgLYhbE+aDIJBKI0oA9UbIEIe19/+ODYJgpAHHOtJx4izmsgaVag==",
"version": "4.22.1",
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.1.tgz",
"integrity": "sha512-NTR3f6W7/AWXKw8GSsgSyQcDW6jkslZLH8AiZa5PQ09w1kR8uHtR9rez/E9gAq/o17+p0JYHE8QjF3RoniiObA==",
"dependencies": {
"@babel/runtime": "^7.20.13",
"@panva/hkdf": "^1.0.2",
@ -11147,9 +11147,9 @@
}
},
"next-auth": {
"version": "4.22.0",
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.0.tgz",
"integrity": "sha512-08+kjnDoE7aQ52O996x6cwA3ffc2CbHIkrCgLYhbE+aDIJBKI0oA9UbIEIe19/+ODYJgpAHHOtJx4izmsgaVag==",
"version": "4.22.1",
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.1.tgz",
"integrity": "sha512-NTR3f6W7/AWXKw8GSsgSyQcDW6jkslZLH8AiZa5PQ09w1kR8uHtR9rez/E9gAq/o17+p0JYHE8QjF3RoniiObA==",
"requires": {
"@babel/runtime": "^7.20.13",
"@panva/hkdf": "^1.0.2",

View file

@ -42,7 +42,7 @@
"micro": "^10.0.1",
"micro-cors": "^0.1.1",
"next": "^13.3.0",
"next-auth": "^4.21.0",
"next-auth": "^4.22.1",
"posthog-js": "^1.53.4",
"posthog-node": "^3.1.0",
"react": "18.2.0",

View file

@ -9,6 +9,7 @@ import GitHubProvider from "next-auth/providers/github";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { env } from "~/env.mjs";
import { prisma } from "~/server/db";
import { PostHog } from "posthog-node";
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
@ -67,6 +68,44 @@ export const authOptions: NextAuthOptions = {
* @see https://next-auth.js.org/providers/github
*/
],
events: {
async signIn(message) {
const client = new PostHog(env.NEXT_PUBLIC_POSTHOG_KEY, {
host: env.NEXT_PUBLIC_POSTHOG_HOST,
});
client.capture({
distinctId: message.user.id,
event: "user logged in",
properties: {
provider: message.account?.provider,
isNewUser: message.isNewUser,
},
});
await client.shutdownAsync();
},
async signOut(message) {
const session = message.session as unknown as {
id: string;
sessionToken: string;
userId: string;
expires: Date;
};
if (!session?.userId) return;
const client = new PostHog(env.NEXT_PUBLIC_POSTHOG_KEY, {
host: env.NEXT_PUBLIC_POSTHOG_HOST,
});
client.capture({
distinctId: session.userId,
event: "user logged out",
});
await client.shutdownAsync();
},
},
pages: {
signIn: "/sign-in",
},