11import { runs } from '@trigger.dev/sdk'
22import { type NextRequest , NextResponse } from 'next/server'
3- import { authenticateApiKeyFromHeader , updateApiKeyLastUsed } from '@/lib/api-key/service'
4- import { getSession } from '@/lib/auth'
3+ import { checkHybridAuth } from '@/lib/auth/hybrid'
54import { generateRequestId } from '@/lib/core/utils/request'
65import { createLogger } from '@/lib/logs/console/logger'
76import { createErrorResponse } from '@/app/api/workflows/utils'
@@ -18,38 +17,44 @@ export async function GET(
1817 try {
1918 logger . debug ( `[${ requestId } ] Getting status for task: ${ taskId } ` )
2019
21- // Try session auth first (for web UI)
22- const session = await getSession ( )
23- let authenticatedUserId : string | null = session ?. user ?. id || null
24-
25- if ( ! authenticatedUserId ) {
26- const apiKeyHeader = request . headers . get ( 'x-api-key' )
27- if ( apiKeyHeader ) {
28- const authResult = await authenticateApiKeyFromHeader ( apiKeyHeader )
29- if ( authResult . success && authResult . userId ) {
30- authenticatedUserId = authResult . userId
31- if ( authResult . keyId ) {
32- await updateApiKeyLastUsed ( authResult . keyId ) . catch ( ( error ) => {
33- logger . warn ( `[${ requestId } ] Failed to update API key last used timestamp:` , {
34- keyId : authResult . keyId ,
35- error,
36- } )
37- } )
38- }
39- }
40- }
20+ const authResult = await checkHybridAuth ( request , { requireWorkflowId : false } )
21+ if ( ! authResult . success || ! authResult . userId ) {
22+ logger . warn ( `[${ requestId } ] Unauthorized task status request` )
23+ return createErrorResponse ( authResult . error || 'Authentication required' , 401 )
4124 }
4225
43- if ( ! authenticatedUserId ) {
44- return createErrorResponse ( 'Authentication required' , 401 )
45- }
26+ const authenticatedUserId = authResult . userId
4627
47- // Fetch task status from Trigger.dev
4828 const run = await runs . retrieve ( taskId )
4929
5030 logger . debug ( `[${ requestId } ] Task ${ taskId } status: ${ run . status } ` )
5131
52- // Map Trigger.dev status to our format
32+ const payload = run . payload as any
33+ if ( payload ?. workflowId ) {
34+ const { verifyWorkflowAccess } = await import ( '@/socket-server/middleware/permissions' )
35+ const accessCheck = await verifyWorkflowAccess ( authenticatedUserId , payload . workflowId )
36+ if ( ! accessCheck . hasAccess ) {
37+ logger . warn ( `[${ requestId } ] User ${ authenticatedUserId } denied access to task ${ taskId } ` , {
38+ workflowId : payload . workflowId ,
39+ } )
40+ return createErrorResponse ( 'Access denied' , 403 )
41+ }
42+ logger . debug ( `[${ requestId } ] User ${ authenticatedUserId } has access to task ${ taskId } ` )
43+ } else {
44+ if ( payload ?. userId && payload . userId !== authenticatedUserId ) {
45+ logger . warn (
46+ `[${ requestId } ] User ${ authenticatedUserId } attempted to access task ${ taskId } owned by ${ payload . userId } `
47+ )
48+ return createErrorResponse ( 'Access denied' , 403 )
49+ }
50+ if ( ! payload ?. userId ) {
51+ logger . warn (
52+ `[${ requestId } ] Task ${ taskId } has no ownership information in payload. Denying access for security.`
53+ )
54+ return createErrorResponse ( 'Access denied' , 403 )
55+ }
56+ }
57+
5358 const statusMap = {
5459 QUEUED : 'queued' ,
5560 WAITING_FOR_DEPLOY : 'queued' ,
@@ -67,7 +72,6 @@ export async function GET(
6772
6873 const mappedStatus = statusMap [ run . status as keyof typeof statusMap ] || 'unknown'
6974
70- // Build response based on status
7175 const response : any = {
7276 success : true ,
7377 taskId,
@@ -77,21 +81,18 @@ export async function GET(
7781 } ,
7882 }
7983
80- // Add completion details if finished
8184 if ( mappedStatus === 'completed' ) {
8285 response . output = run . output // This contains the workflow execution results
8386 response . metadata . completedAt = run . finishedAt
8487 response . metadata . duration = run . durationMs
8588 }
8689
87- // Add error details if failed
8890 if ( mappedStatus === 'failed' ) {
8991 response . error = run . error
9092 response . metadata . completedAt = run . finishedAt
9193 response . metadata . duration = run . durationMs
9294 }
9395
94- // Add progress info if still processing
9596 if ( mappedStatus === 'processing' || mappedStatus === 'queued' ) {
9697 response . estimatedDuration = 180000 // 3 minutes max from our config
9798 }
@@ -107,6 +108,3 @@ export async function GET(
107108 return createErrorResponse ( 'Failed to fetch task status' , 500 )
108109 }
109110}
110-
111- // TODO: Implement task cancellation via Trigger.dev API if needed
112- // export async function DELETE() { ... }
0 commit comments