add sign-in and sign-out events
This commit is contained in:
parent
c3dad32a2e
commit
9ad8bcf507
3 changed files with 47 additions and 8 deletions
14
package-lock.json
generated
14
package-lock.json
generated
|
|
@ -34,7 +34,7 @@
|
||||||
"micro": "^10.0.1",
|
"micro": "^10.0.1",
|
||||||
"micro-cors": "^0.1.1",
|
"micro-cors": "^0.1.1",
|
||||||
"next": "^13.3.0",
|
"next": "^13.3.0",
|
||||||
"next-auth": "^4.21.0",
|
"next-auth": "^4.22.1",
|
||||||
"posthog-js": "^1.53.4",
|
"posthog-js": "^1.53.4",
|
||||||
"posthog-node": "^3.1.0",
|
"posthog-node": "^3.1.0",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
|
|
@ -5350,9 +5350,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/next-auth": {
|
"node_modules/next-auth": {
|
||||||
"version": "4.22.0",
|
"version": "4.22.1",
|
||||||
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.0.tgz",
|
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.1.tgz",
|
||||||
"integrity": "sha512-08+kjnDoE7aQ52O996x6cwA3ffc2CbHIkrCgLYhbE+aDIJBKI0oA9UbIEIe19/+ODYJgpAHHOtJx4izmsgaVag==",
|
"integrity": "sha512-NTR3f6W7/AWXKw8GSsgSyQcDW6jkslZLH8AiZa5PQ09w1kR8uHtR9rez/E9gAq/o17+p0JYHE8QjF3RoniiObA==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime": "^7.20.13",
|
"@babel/runtime": "^7.20.13",
|
||||||
"@panva/hkdf": "^1.0.2",
|
"@panva/hkdf": "^1.0.2",
|
||||||
|
|
@ -11147,9 +11147,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"next-auth": {
|
"next-auth": {
|
||||||
"version": "4.22.0",
|
"version": "4.22.1",
|
||||||
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.0.tgz",
|
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-4.22.1.tgz",
|
||||||
"integrity": "sha512-08+kjnDoE7aQ52O996x6cwA3ffc2CbHIkrCgLYhbE+aDIJBKI0oA9UbIEIe19/+ODYJgpAHHOtJx4izmsgaVag==",
|
"integrity": "sha512-NTR3f6W7/AWXKw8GSsgSyQcDW6jkslZLH8AiZa5PQ09w1kR8uHtR9rez/E9gAq/o17+p0JYHE8QjF3RoniiObA==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@babel/runtime": "^7.20.13",
|
"@babel/runtime": "^7.20.13",
|
||||||
"@panva/hkdf": "^1.0.2",
|
"@panva/hkdf": "^1.0.2",
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
"micro": "^10.0.1",
|
"micro": "^10.0.1",
|
||||||
"micro-cors": "^0.1.1",
|
"micro-cors": "^0.1.1",
|
||||||
"next": "^13.3.0",
|
"next": "^13.3.0",
|
||||||
"next-auth": "^4.21.0",
|
"next-auth": "^4.22.1",
|
||||||
"posthog-js": "^1.53.4",
|
"posthog-js": "^1.53.4",
|
||||||
"posthog-node": "^3.1.0",
|
"posthog-node": "^3.1.0",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import GitHubProvider from "next-auth/providers/github";
|
||||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||||
import { env } from "~/env.mjs";
|
import { env } from "~/env.mjs";
|
||||||
import { prisma } from "~/server/db";
|
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`
|
* 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
|
* @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: {
|
pages: {
|
||||||
signIn: "/sign-in",
|
signIn: "/sign-in",
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue