1- import { db } from '@sim/db'
2- import { workflow } from '@sim/db/schema'
31import { createLogger } from '@sim/logger'
4- import { eq } from 'drizzle-orm'
52import type { NextRequest } from 'next/server'
63import { authenticateApiKeyFromHeader , updateApiKeyLastUsed } from '@/lib/api-key/service'
74import { getSession } from '@/lib/auth'
@@ -13,35 +10,33 @@ export interface AuthResult {
1310 success : boolean
1411 userId ?: string
1512 authType ?: 'session' | 'api_key' | 'internal_jwt'
13+ apiKeyType ?: 'personal' | 'workspace'
1614 error ?: string
1715}
1816
1917/**
2018 * Resolves userId from a verified internal JWT token.
21- * Extracts workflowId/ userId from URL params or POST body, then looks up userId if needed .
19+ * Extracts userId from the JWT payload, URL search params, or POST body .
2220 */
2321async function resolveUserFromJwt (
2422 request : NextRequest ,
2523 verificationUserId : string | null ,
2624 options : { requireWorkflowId ?: boolean }
2725) : Promise < AuthResult > {
28- let workflowId : string | null = null
2926 let userId : string | null = verificationUserId
3027
31- const { searchParams } = new URL ( request . url )
32- workflowId = searchParams . get ( 'workflowId' )
3328 if ( ! userId ) {
29+ const { searchParams } = new URL ( request . url )
3430 userId = searchParams . get ( 'userId' )
3531 }
3632
37- if ( ! workflowId && ! userId && request . method === 'POST' ) {
33+ if ( ! userId && request . method === 'POST' ) {
3834 try {
3935 const clonedRequest = request . clone ( )
4036 const bodyText = await clonedRequest . text ( )
4137 if ( bodyText ) {
4238 const body = JSON . parse ( bodyText )
43- workflowId = body . workflowId || body . _context ?. workflowId
44- userId = userId || body . userId || body . _context ?. userId
39+ userId = body . userId || body . _context ?. userId || null
4540 }
4641 } catch {
4742 // Ignore JSON parse errors
@@ -52,22 +47,8 @@ async function resolveUserFromJwt(
5247 return { success : true , userId, authType : 'internal_jwt' }
5348 }
5449
55- if ( workflowId ) {
56- const [ workflowData ] = await db
57- . select ( { userId : workflow . userId } )
58- . from ( workflow )
59- . where ( eq ( workflow . id , workflowId ) )
60- . limit ( 1 )
61-
62- if ( ! workflowData ) {
63- return { success : false , error : 'Workflow not found' }
64- }
65-
66- return { success : true , userId : workflowData . userId , authType : 'internal_jwt' }
67- }
68-
6950 if ( options . requireWorkflowId !== false ) {
70- return { success : false , error : 'workflowId or userId required for internal JWT calls' }
51+ return { success : false , error : 'userId required for internal JWT calls' }
7152 }
7253
7354 return { success : true , authType : 'internal_jwt' }
@@ -222,6 +203,7 @@ export async function checkHybridAuth(
222203 success : true ,
223204 userId : result . userId ! ,
224205 authType : 'api_key' ,
206+ apiKeyType : result . keyType ,
225207 }
226208 }
227209
0 commit comments