11import { NextResponse } from 'next/server' ;
22import { createClient } from '@supabase/supabase-js' ;
3+
4+ const supabaseAuth = createClient ( process . env . NEXT_PUBLIC_SUPABASE_URL , process . env . NEXT_PUBLIC_SUPABASE_ANON_KEY ) ;
5+
6+ async function authenticateBearerToken ( request ) {
7+ const authHeader = request . headers . get ( 'authorization' ) ;
8+ if ( ! authHeader || ! authHeader . startsWith ( 'Bearer ' ) ) {
9+ return { error : 'Authentication required' } ;
10+ }
11+
12+ const token = authHeader . substring ( 7 ) ;
13+ const { data : { user } , error } = await supabaseAuth . auth . getUser ( token ) ;
14+
15+ if ( error || ! user ?. id ) {
16+ return { error : 'Invalid authentication token' } ;
17+ }
18+
19+ return { user } ;
20+ }
21+
322export async function POST ( request ) {
423 try {
5- // Get the authorization header
6- const authHeader = request . headers . get ( 'authorization' ) ;
7- if ( ! authHeader || ! authHeader . startsWith ( 'Bearer ' ) ) {
8- return NextResponse . json ( { error : 'Authentication required' } , { status : 401 } ) ;
9- }
10-
11- const token = authHeader . replace ( 'Bearer ' , '' ) ;
12-
13- // Decode JWT token to get user ID (simple validation)
14- let user ;
15- try {
16- // JWT tokens have 3 parts separated by dots: header.payload.signature
17- const tokenParts = token . split ( '.' ) ;
18- if ( tokenParts . length !== 3 ) {
19- throw new Error ( 'Invalid token format' ) ;
20- }
21-
22- // Decode the payload (second part)
23- const payload = JSON . parse ( atob ( tokenParts [ 1 ] ) ) ;
24-
25- // Check if token is expired
26- if ( payload . exp && payload . exp < Date . now ( ) / 1000 ) {
27- throw new Error ( 'Token expired' ) ;
28- }
29-
30- // Extract user info from payload
31- user = {
32- id : payload . sub ,
33- email : payload . email
34- } ;
35-
36- if ( ! user . id ) {
37- throw new Error ( 'Invalid token payload' ) ;
38- }
39- } catch ( error ) {
40- console . error ( 'Token validation error:' , error ) ;
41- return NextResponse . json ( { error : 'Invalid authentication token' } , { status : 401 } ) ;
24+ const { user, error : authError } = await authenticateBearerToken ( request ) ;
25+ if ( authError || ! user ) {
26+ return NextResponse . json ( { error : authError } , { status : 401 } ) ;
4227 }
4328
4429 // Create service role client for database operations
@@ -74,7 +59,7 @@ export async function POST(request) {
7459 const fileBuffer = await file . arrayBuffer ( ) ;
7560
7661 // Upload to Supabase Storage
77- const { data : uploadData , error : uploadError } = await supabase . storage
62+ const { error : uploadError } = await supabase . storage
7863 . from ( 'avatars' )
7964 . upload ( fileName , fileBuffer , {
8065 contentType : file . type ,
@@ -119,43 +104,9 @@ export async function POST(request) {
119104
120105export async function DELETE ( request ) {
121106 try {
122- // Get the authorization header
123- const authHeader = request . headers . get ( 'authorization' ) ;
124- if ( ! authHeader || ! authHeader . startsWith ( 'Bearer ' ) ) {
125- return NextResponse . json ( { error : 'Authentication required' } , { status : 401 } ) ;
126- }
127-
128- const token = authHeader . replace ( 'Bearer ' , '' ) ;
129-
130- // Decode JWT token to get user ID (simple validation)
131- let user ;
132- try {
133- // JWT tokens have 3 parts separated by dots: header.payload.signature
134- const tokenParts = token . split ( '.' ) ;
135- if ( tokenParts . length !== 3 ) {
136- throw new Error ( 'Invalid token format' ) ;
137- }
138-
139- // Decode the payload (second part)
140- const payload = JSON . parse ( atob ( tokenParts [ 1 ] ) ) ;
141-
142- // Check if token is expired
143- if ( payload . exp && payload . exp < Date . now ( ) / 1000 ) {
144- throw new Error ( 'Token expired' ) ;
145- }
146-
147- // Extract user info from payload
148- user = {
149- id : payload . sub ,
150- email : payload . email
151- } ;
152-
153- if ( ! user . id ) {
154- throw new Error ( 'Invalid token payload' ) ;
155- }
156- } catch ( error ) {
157- console . error ( 'Token validation error:' , error ) ;
158- return NextResponse . json ( { error : 'Invalid authentication token' } , { status : 401 } ) ;
107+ const { user, error : authError } = await authenticateBearerToken ( request ) ;
108+ if ( authError || ! user ) {
109+ return NextResponse . json ( { error : authError } , { status : 401 } ) ;
159110 }
160111
161112 // Create service role client for storage and database operations
@@ -184,4 +135,4 @@ export async function DELETE(request) {
184135 console . error ( 'Avatar removal error:' , error ) ;
185136 return NextResponse . json ( { error : 'Internal server error' } , { status : 500 } ) ;
186137 }
187- }
138+ }
0 commit comments