11import { NextRequest , NextResponse } from 'next/server'
2- import { createClient } from '@supabase/ supabase-js '
2+ import { createServerClient } from '@/lib/ supabase-server '
33
44export const dynamic = 'force-dynamic'
55
@@ -38,26 +38,20 @@ function buildFileTree(files: any[]): any[] {
3838
3939export async function GET ( request : NextRequest ) {
4040 try {
41- const sessionID = request . nextUrl . searchParams . get ( 'sessionID' )
41+ const supabase = createServerClient ( )
4242
43- if ( ! sessionID ) {
44- return NextResponse . json ( { error : 'Session ID is required' } , { status : 400 } )
45- }
46-
47- const supabaseUrl = process . env . NEXT_PUBLIC_SUPABASE_URL
48- const supabaseKey = process . env . SUPABASE_SERVICE_ROLE_KEY
43+ // Get authenticated user from session
44+ const { data : { user } , error : authError } = await supabase . auth . getUser ( )
4945
50- if ( ! supabaseUrl || ! supabaseKey ) {
51- return NextResponse . json ( { error : 'Supabase configuration missing ' } , { status : 500 } )
46+ if ( authError || ! user ) {
47+ return NextResponse . json ( { error : 'Unauthorized ' } , { status : 401 } )
5248 }
5349
54- const supabase = createClient ( supabaseUrl , supabaseKey )
55-
5650 // Fetch all workspace files for the user
5751 const { data : files , error } = await supabase
5852 . from ( 'workspace_files' )
5953 . select ( '*' )
60- . eq ( 'user_id' , sessionID )
54+ . eq ( 'user_id' , user . id )
6155 . order ( 'path' , { ascending : true } )
6256
6357 if ( error ) {
@@ -78,20 +72,20 @@ export async function GET(request: NextRequest) {
7872export async function POST ( request : NextRequest ) {
7973 try {
8074 const body = await request . json ( )
81- const { sessionID , path, isDirectory, content = '' } = body
75+ const { path, isDirectory, content = '' } = body
8276
83- if ( ! sessionID || ! path ) {
84- return NextResponse . json ( { error : 'Session ID and path are required' } , { status : 400 } )
77+ if ( ! path ) {
78+ return NextResponse . json ( { error : 'Path is required' } , { status : 400 } )
8579 }
8680
87- const supabaseUrl = process . env . NEXT_PUBLIC_SUPABASE_URL
88- const supabaseKey = process . env . SUPABASE_SERVICE_ROLE_KEY
81+ const supabase = createServerClient ( )
8982
90- if ( ! supabaseUrl || ! supabaseKey ) {
91- return NextResponse . json ( { error : 'Supabase configuration missing' } , { status : 500 } )
92- }
83+ // Get authenticated user from session
84+ const { data : { user } , error : authError } = await supabase . auth . getUser ( )
9385
94- const supabase = createClient ( supabaseUrl , supabaseKey )
86+ if ( authError || ! user ) {
87+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
88+ }
9589
9690 // Extract file name and parent path
9791 const pathParts = path . split ( '/' )
@@ -105,7 +99,7 @@ export async function POST(request: NextRequest) {
10599 const { data : file , error } = await supabase
106100 . from ( 'workspace_files' )
107101 . insert ( {
108- user_id : sessionID ,
102+ user_id : user . id ,
109103 path,
110104 name,
111105 content,
@@ -131,26 +125,26 @@ export async function POST(request: NextRequest) {
131125export async function DELETE ( request : NextRequest ) {
132126 try {
133127 const body = await request . json ( )
134- const { sessionID , path } = body
128+ const { path } = body
135129
136- if ( ! sessionID || ! path ) {
137- return NextResponse . json ( { error : 'Session ID and path are required' } , { status : 400 } )
130+ if ( ! path ) {
131+ return NextResponse . json ( { error : 'Path is required' } , { status : 400 } )
138132 }
139133
140- const supabaseUrl = process . env . NEXT_PUBLIC_SUPABASE_URL
141- const supabaseKey = process . env . SUPABASE_SERVICE_ROLE_KEY
134+ const supabase = createServerClient ( )
142135
143- if ( ! supabaseUrl || ! supabaseKey ) {
144- return NextResponse . json ( { error : 'Supabase configuration missing' } , { status : 500 } )
145- }
136+ // Get authenticated user from session
137+ const { data : { user } , error : authError } = await supabase . auth . getUser ( )
146138
147- const supabase = createClient ( supabaseUrl , supabaseKey )
139+ if ( authError || ! user ) {
140+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
141+ }
148142
149143 // Delete the file and all its children (for directories)
150144 const { error } = await supabase
151145 . from ( 'workspace_files' )
152146 . delete ( )
153- . eq ( 'user_id' , sessionID )
147+ . eq ( 'user_id' , user . id )
154148 . or ( `path.eq.${ path } ,parent_path.eq.${ path } ` )
155149
156150 if ( error ) {
0 commit comments