(
user: Pick<
User,
| 'id'
| 'google_user_email'
| 'created_at'
| 'hosted_domain'
| 'signup_ip'
| 'is_admin'
| 'is_bot'
| 'blocked_at'
| 'completed_welcome_form'
| 'linkedin_url'
| 'github_url'
| 'discord_server_membership_verified_at'
| 'customer_source'
| 'cohorts'
| 'has_validation_stytch'
| 'has_validation_novel_card_with_hold'
>,
eventType: 'signup' | 'signin',
provider: AuthProviderId,
requestHeaders?: Headers
)
| 327 | return (await findUserById(args.provider_account_id)) ?? null; |
| 328 | } |
| 329 | |
| 330 | const existing_kilo_user_id = await findUserIdByAuthProvider( |
| 331 | args.provider, |
| 332 | args.provider_account_id |
| 333 | ); |
| 334 | if (!existing_kilo_user_id) { |
| 335 | return null; |
| 336 | } |
| 337 | |
| 338 | const existingUser = await findUserById(existing_kilo_user_id); |
| 339 | assert(existingUser, `User not found for kiloUserId: ${existing_kilo_user_id}`); |
| 340 | |
| 341 | if (existingUser.hosted_domain !== args.hosted_domain) { |
| 342 | //This really should only affect legacy users. |
| 343 | await db |
| 344 | .update(kilocode_users) |
| 345 | .set({ hosted_domain: args.hosted_domain }) |
| 346 | .where(eq(kilocode_users.id, existingUser.id)); |
| 347 | console.log( |
| 348 | `Updated hosted_domain for user ${existingUser.id}: ${existingUser.hosted_domain} -> ${args.hosted_domain}` |
| 349 | ); |
| 350 | existingUser.hosted_domain = args.hosted_domain; |
| 351 | } |
| 352 | |
| 353 | // Sync display_name from OAuth on every sign-in |
| 354 | if (args.display_name) { |
| 355 | await db |
| 356 | .update(user_auth_provider) |
| 357 | .set({ display_name: args.display_name }) |
| 358 | .where( |
| 359 | and( |
| 360 | eq(user_auth_provider.kilo_user_id, existingUser.id), |
| 361 | eq(user_auth_provider.provider, args.provider), |
| 362 | eq(user_auth_provider.provider_account_id, args.provider_account_id) |
| 363 | ) |
| 364 | ); |
| 365 | } |
| 366 | |
| 367 | timer.log(`findFirst user with id ${existingUser.id}`); |
| 368 | return existingUser; |
| 369 | } |
| 370 | |
| 371 | export async function findUserByEmail(email: string): Promise<User | undefined> { |
| 372 | return await db.query.kilocode_users.findFirst({ |
| 373 | where: eq(kilocode_users.google_user_email, email), |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | export async function findUserByEmailCaseInsensitive(email: string): Promise<User[]> { |
| 378 | return db.query.kilocode_users.findMany({ |
| 379 | where: eq(sql`lower(${kilocode_users.google_user_email})`, email.trim().toLowerCase()), |
| 380 | }); |
| 381 | } |
| 382 | |
| 383 | export async function findUserByNormalizedEmail(email: string): Promise<User | undefined> { |
| 384 | const normalizedEmail = normalizeEmail(email); |
| 385 | return await db.query.kilocode_users.findFirst({ |
| 386 | where: or( |
no test coverage detected