1+ import { db } from '@sim/db'
2+ import { user } from '@sim/db/schema'
13import { createLogger } from '@sim/logger'
4+ import { eq } from 'drizzle-orm'
25import type { NextRequest } from 'next/server'
36import { authenticateApiKeyFromHeader , updateApiKeyLastUsed } from '@/lib/api-key/service'
47import { getSession } from '@/lib/auth'
@@ -16,6 +19,25 @@ export interface AuthResult {
1619 error ?: string
1720}
1821
22+ /**
23+ * Looks up a user's name and email by ID. Returns empty values on failure
24+ * so auth is never blocked by a lookup error.
25+ */
26+ async function lookupUserInfo (
27+ userId : string
28+ ) : Promise < { userName : string | null ; userEmail : string | null } > {
29+ try {
30+ const [ row ] = await db
31+ . select ( { name : user . name , email : user . email } )
32+ . from ( user )
33+ . where ( eq ( user . id , userId ) )
34+ . limit ( 1 )
35+ return { userName : row ?. name ?? null , userEmail : row ?. email ?? null }
36+ } catch {
37+ return { userName : null , userEmail : null }
38+ }
39+ }
40+
1941/**
2042 * Resolves userId from a verified internal JWT token.
2143 * Extracts userId from the JWT payload, URL search params, or POST body.
@@ -46,7 +68,8 @@ async function resolveUserFromJwt(
4668 }
4769
4870 if ( userId ) {
49- return { success : true , userId, authType : 'internal_jwt' }
71+ const { userName, userEmail } = await lookupUserInfo ( userId )
72+ return { success : true , userId, userName, userEmail, authType : 'internal_jwt' }
5073 }
5174
5275 if ( options . requireWorkflowId !== false ) {
@@ -205,9 +228,12 @@ export async function checkHybridAuth(
205228 const result = await authenticateApiKeyFromHeader ( apiKeyHeader )
206229 if ( result . success ) {
207230 await updateApiKeyLastUsed ( result . keyId ! )
231+ const { userName, userEmail } = await lookupUserInfo ( result . userId ! )
208232 return {
209233 success : true ,
210234 userId : result . userId ! ,
235+ userName,
236+ userEmail,
211237 authType : 'api_key' ,
212238 apiKeyType : result . keyType ,
213239 }
0 commit comments