()
| 14 | * Called when DISABLE_AUTH is enabled to ensure DB operations work. |
| 15 | */ |
| 16 | export async function ensureAnonymousUserExists(): Promise<void> { |
| 17 | if (anonymousUserEnsured) return |
| 18 | |
| 19 | try { |
| 20 | const existingUser = await db.query.user.findFirst({ |
| 21 | where: eq(schema.user.id, ANONYMOUS_USER_ID), |
| 22 | }) |
| 23 | |
| 24 | if (!existingUser) { |
| 25 | const now = new Date() |
| 26 | await db.insert(schema.user).values({ |
| 27 | ...ANONYMOUS_USER, |
| 28 | createdAt: now, |
| 29 | updatedAt: now, |
| 30 | }) |
| 31 | logger.info('Created anonymous user for DISABLE_AUTH mode') |
| 32 | } |
| 33 | |
| 34 | const existingStats = await db.query.userStats.findFirst({ |
| 35 | where: eq(schema.userStats.userId, ANONYMOUS_USER_ID), |
| 36 | }) |
| 37 | |
| 38 | if (!existingStats) { |
| 39 | await db.insert(schema.userStats).values({ |
| 40 | id: generateId(), |
| 41 | userId: ANONYMOUS_USER_ID, |
| 42 | currentUsageLimit: '10000000000', |
| 43 | }) |
| 44 | logger.info('Created anonymous user stats for DISABLE_AUTH mode') |
| 45 | } |
| 46 | |
| 47 | anonymousUserEnsured = true |
| 48 | } catch (error) { |
| 49 | if ( |
| 50 | error instanceof Error && |
| 51 | (error.message.includes('unique') || error.message.includes('duplicate')) |
| 52 | ) { |
| 53 | anonymousUserEnsured = true |
| 54 | return |
| 55 | } |
| 56 | logger.error('Failed to ensure anonymous user exists', { error }) |
| 57 | throw error |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export interface AnonymousSession { |
| 62 | user: { |
no test coverage detected