@@ -11,20 +11,36 @@ import { NextResponse } from 'next/server'
1111import { getServerSession } from 'next-auth'
1212
1313import { authOptions } from '@/app/api/auth/[...nextauth]/auth-options'
14+ import { extractApiKeyFromHeader , getUserIdFromSessionToken } from '@/util/auth'
1415import { logger } from '@/util/logger'
1516
1617import type {
1718 NoSubscriptionResponse ,
1819 ActiveSubscriptionResponse ,
1920} from '@codebuff/common/types/subscription'
21+ import type { NextRequest } from 'next/server'
2022
21- export async function GET ( ) {
22- const session = await getServerSession ( authOptions )
23- if ( ! session ?. user ?. id ) {
24- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
23+ export async function GET ( req : NextRequest ) {
24+ let userId : string | undefined
25+
26+ // First, try Bearer token authentication (for CLI clients)
27+ const apiKey = extractApiKeyFromHeader ( req )
28+ if ( apiKey ) {
29+ const userIdFromToken = await getUserIdFromSessionToken ( apiKey )
30+ if ( userIdFromToken ) {
31+ userId = userIdFromToken
32+ }
33+ }
34+
35+ // Fall back to NextAuth session authentication (for web clients)
36+ if ( ! userId ) {
37+ const session = await getServerSession ( authOptions )
38+ userId = session ?. user ?. id
2539 }
2640
27- const userId = session . user . id
41+ if ( ! userId ) {
42+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
43+ }
2844
2945 // Fetch user preference for always use a-la-carte
3046 const [ subscription , userPrefs ] = await Promise . all ( [
0 commit comments