🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Enhance authentication logging and improve backfill logic for identified user IDs
Bill Yang188c8311c076
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.tsindex 1263ecbb0..90f8972c8 100644--- a/server/src/lib/auth.ts+++ b/server/src/lib/auth.ts@@ -22,9 +22,12 @@ import {} from "./email/email.js";import { onboardingTipsService } from "../services/onboardingTips/onboardingTipsService.js";import { getTrustedCorsOrigins } from "./cors.js";+import { createServiceLogger } from "./logger/logger.js";dotenv.config();+const authLogger = createServiceLogger("better-auth");+const pluginList = [admin(),apiKey({@@ -181,6 +184,13 @@ const pluginList = [export const auth = betterAuth({basePath: "/api/auth",appName: "Rybbit",+ logger: {+ log: (level, message, ...args) => {+ // Route better-auth's internal logs (e.g. API key rate-limit errors)+ // through the project's pino logger instead of console.+ authLogger[level]({ args }, message);+ },+ },database: new pg.Pool({host: process.env.POSTGRES_HOST || "postgres",port: parseInt(process.env.POSTGRES_PORT || "5432", 10),diff --git a/server/src/services/tracker/identifyService.ts b/server/src/services/tracker/identifyService.tsindex b2c681e58..114556dee 100644--- a/server/src/services/tracker/identifyService.ts+++ b/server/src/services/tracker/identifyService.ts@@ -41,10 +41,17 @@ const BACKFILL_DAYS = 30;async function backfillIdentifiedUserId(siteId: number, anonymousId: string, userId: string) {try {- const tables = ["events", "session_replay_events", "session_replay_metadata"];- for (const table of tables) {+ // session_replay_metadata has no `timestamp` column; its time column is+ // `start_time`. Using `timestamp` there throws ClickHouse error 47+ // (UNKNOWN_IDENTIFIER), so map each table to its actual time column.+ const tables: Array<{ name: string; timeColumn: string }> = [+ { name: "events", timeColumn: "timestamp" },+ { name: "session_replay_events", timeColumn: "timestamp" },+ { name: "session_replay_metadata", timeColumn: "start_time" },+ ];+ for (const { name, timeColumn } of tables) {await clickhouse.command({- query: `ALTER TABLE ${table} UPDATE identified_user_id = {userId: String} WHERE site_id = {siteId: UInt16} AND user_id = {anonymousId: String} AND identified_user_id = '' AND timestamp >= now() - INTERVAL {days: UInt16} DAY`,+ query: `ALTER TABLE ${name} UPDATE identified_user_id = {userId: String} WHERE site_id = {siteId: UInt16} AND user_id = {anonymousId: String} AND identified_user_id = '' AND ${timeColumn} >= now() - INTERVAL {days: UInt16} DAY`,query_params: { userId, siteId, anonymousId, days: BACKFILL_DAYS },});}