Skip to content

Commit

Permalink
Track login and logout
Browse files Browse the repository at this point in the history
  • Loading branch information
RobVermeer committed Dec 22, 2023
1 parent c215951 commit ec1f39f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/app/[locale]/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NextAuth, { AuthOptions, Session, User } from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import EmailProvider from "next-auth/providers/email"
import { sendVerificationRequest } from "@/utils/send-verification-request"
import { trackIssue } from "@/lib/trackIssue"

export const authOptions: AuthOptions = {
adapter: PrismaAdapter(prisma),
Expand Down Expand Up @@ -38,14 +39,14 @@ export const authOptions: AuthOptions = {
},
events: {
signIn: async ({ profile, user, isNewUser }) => {
console.log("User sign in", { ...user, isNewUser })
trackIssue("User sign in", "info", { ...user, isNewUser })

if (!isNewUser && !user.firstName && profile?.firstName) {
await updateFirstNameById(user.id, profile.firstName)
}
},
signOut: ({ session }) => {
console.log("User sign out", session)
trackIssue("User sign out", "info", session.user)
},
},
}
Expand Down
23 changes: 23 additions & 0 deletions src/lib/trackIssue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use server"

const trackIssueUrl = process.env.TRACK_ISSUE_URL

type Level = "error" | "debug" | "info" | "warn" | "fatal"

export async function trackIssue(
message: string,
level: Level = "error",
extra?: Record<string, any>
) {
if (!trackIssueUrl) return

await fetch(trackIssueUrl, {
method: "post",
body: JSON.stringify({
message,
level,
extra,
}),
cache: "no-cache",
})
}

0 comments on commit ec1f39f

Please sign in to comment.