1- import { NextRequest , NextResponse } from 'next/server'
2- import { createClient , type User } from '@supabase/supabase-js'
3- import { jwtVerify , createRemoteJWKSet } from 'jose'
1+ import { NextRequest , NextResponse } from 'next/server' ;
2+ import { createClient , type User } from '@supabase/supabase-js' ;
3+ import { jwtVerify , createRemoteJWKSet } from 'jose' ;
44
5- const supabaseUrl = process . env . NEXT_PUBLIC_SUPABASE_URL || process . env . SUPABASE_URL
6- const supabaseAnonKey = process . env . NEXT_PUBLIC_SUPABASE_ANON_KEY || process . env . SUPABASE_ANON_KEY
5+ const supabaseUrl =
6+ process . env . NEXT_PUBLIC_SUPABASE_URL || process . env . SUPABASE_URL ;
7+ const supabaseAnonKey =
8+ process . env . NEXT_PUBLIC_SUPABASE_ANON_KEY || process . env . SUPABASE_ANON_KEY ;
79
810// Define Para JWKS URLs based on environment
911const PARA_JWKS_URLS = {
@@ -43,11 +45,11 @@ interface ParaJwtPayload {
4345
4446export type AuthResult =
4547 | { success : true ; user : User }
46- | { success : false ; error : NextResponse }
48+ | { success : false ; error : NextResponse } ;
4749
4850export type AuthResultWithHeaders =
4951 | { success : true ; user : User }
50- | { success : false ; error : string }
52+ | { success : false ; error : string } ;
5153
5254// Core verification logic that works with any headers-like object
5355async function verifyAuthCore (
@@ -58,26 +60,27 @@ async function verifyAuthCore(
5860 if ( ! supabaseUrl || ! supabaseAnonKey ) {
5961 return {
6062 success : false ,
61- error : 'Supabase configuration missing'
62- }
63+ error : 'Supabase configuration missing' ,
64+ } ;
6365 }
6466
65- const supabase = createClient ( supabaseUrl , supabaseAnonKey )
67+ const supabase = createClient ( supabaseUrl , supabaseAnonKey ) ;
6668
6769 if ( ! authHeader || ! authHeader . startsWith ( 'Bearer ' ) ) {
6870 return {
6971 success : false ,
70- error : 'Authorization header required'
71- }
72+ error : 'Authorization header required' ,
73+ } ;
7274 }
7375
74- const token = authHeader . replace ( 'Bearer ' , '' )
76+ const token = authHeader . replace ( 'Bearer ' , '' ) ;
7577
7678 if ( authMethod === 'para' ) {
7779 // Verify as Para JWT
7880 try {
7981 // Get JWKS URL based on environment
80- const env = ( process . env . PARA_ENVIRONMENT || 'prod' ) as keyof typeof PARA_JWKS_URLS ;
82+ const env = ( process . env . PARA_ENVIRONMENT ||
83+ 'prod' ) as keyof typeof PARA_JWKS_URLS ;
8184 const jwksUrl = PARA_JWKS_URLS [ env ] ;
8285 const JWKS = createRemoteJWKSet ( new URL ( jwksUrl ) ) ;
8386
@@ -87,7 +90,8 @@ async function verifyAuthCore(
8790 } ) ;
8891
8992 // If Para JWT verification succeeds, create a mock user object
90- const email = payload . data . email || `${ payload . data . userId } @para-fallback.com` ;
93+ const email =
94+ payload . data . email || `${ payload . data . userId } @para-fallback.com` ;
9195 const paraUser : User = {
9296 id : payload . data . userId ,
9397 email,
@@ -107,60 +111,69 @@ async function verifyAuthCore(
107111
108112 return {
109113 success : true ,
110- user : paraUser
114+ user : paraUser ,
111115 } ;
112116 } catch ( paraError ) {
113117 console . log ( 'Para JWT verification failed:' , paraError ) ;
114118 return {
115119 success : false ,
116- error : 'Invalid or expired Para JWT token'
117- }
120+ error : 'Invalid or expired Para JWT token' ,
121+ } ;
118122 }
119123 } else {
120124 // Verify as Supabase JWT
121- const { data : { user } , error : authError } = await supabase . auth . getUser ( token )
125+ const {
126+ data : { user } ,
127+ error : authError ,
128+ } = await supabase . auth . getUser ( token ) ;
122129
123130 if ( authError || ! user ) {
124131 return {
125132 success : false ,
126- error : 'Invalid or expired Supabase token'
127- }
133+ error : 'Invalid or expired Supabase token' ,
134+ } ;
128135 }
129136
130137 console . log ( 'Supabase user:' , user ) ;
131138
132139 return {
133140 success : true ,
134- user
135- }
141+ user,
142+ } ;
136143 }
137144}
138145
139146// For use with NextRequest (API routes, middleware)
140147export async function verifyAuth ( request : NextRequest ) : Promise < AuthResult > {
141- const authHeader = request . headers . get ( 'authorization' )
142- const authMethod = request . headers . get ( 'x-auth-method' )
148+ const authHeader = request . headers . get ( 'authorization' ) ;
149+ const authMethod = request . headers . get ( 'x-auth-method' ) ;
143150
144- const result = await verifyAuthCore ( authHeader , authMethod )
151+ const result = await verifyAuthCore ( authHeader , authMethod ) ;
145152
146153 if ( ! result . success ) {
147154 return {
148155 success : false ,
149- error : NextResponse . json ( { error : result . error } , {
150- status : result . error . includes ( 'configuration' ) ? 500 : 401
151- } )
152- }
156+ error : NextResponse . json (
157+ { error : result . error } ,
158+ {
159+ status : result . error . includes ( 'configuration' ) ? 500 : 401 ,
160+ }
161+ ) ,
162+ } ;
153163 }
154164
155- return result
165+ return result ;
156166}
157167
158168// For use with Headers from Server Components (layouts, pages)
159169export async function verifyAuthWithHeaders (
160170 headers : Headers
161171) : Promise < AuthResultWithHeaders > {
162- const authHeader = headers . get ( 'authorization' )
163- const authMethod = headers . get ( 'x-auth-method' )
172+ const authHeader = headers . get ( 'authorization' ) ;
173+ const authMethod = headers . get ( 'x-auth-method' ) ;
174+
175+ console . log ( authHeader , 'authHeader' ) ;
176+ console . log ( authMethod , 'authMethod' ) ;
164177
165- return verifyAuthCore ( authHeader , authMethod )
178+ return verifyAuthCore ( authHeader , authMethod ) ;
166179}
0 commit comments