@@ -88,20 +88,38 @@ window.addEventListener('podkey-request', async (event) => {
8888
8989 const response = await originalFetch ( url , options ) ;
9090
91- // Handle 401 responses
91+ // Handle 401 responses - retry with NIP-98 auth
9292 if ( response . status === 401 ) {
93+ console . log ( '[Podkey] 401 detected, attempting NIP-98 auth retry for:' , url ) ;
9394 try {
95+ // Clone response to read body if needed, but for retry we'll make a new request
9496 const authHeader = await getNip98AuthHeader ( url , options . method || 'GET' , options . body ) ;
9597 if ( authHeader ) {
9698 // Retry with auth
9799 const retryOptions = { ...options } ;
98100 retryOptions . headers = retryOptions . headers || { } ;
101+
102+ // Handle Headers object
99103 if ( retryOptions . headers instanceof Headers ) {
100104 retryOptions . headers . set ( 'Authorization' , authHeader ) ;
101- } else {
105+ } else if ( retryOptions . headers instanceof Object ) {
102106 retryOptions . headers [ 'Authorization' ] = authHeader ;
107+ } else {
108+ retryOptions . headers = { 'Authorization' : authHeader } ;
103109 }
104- return await originalFetch ( url , retryOptions ) ;
110+
111+ console . log ( '[Podkey] Retrying request with NIP-98 auth' ) ;
112+ const retryResponse = await originalFetch ( url , retryOptions ) ;
113+
114+ if ( retryResponse . status === 200 || retryResponse . status === 201 ) {
115+ console . log ( '[Podkey] ✅ NIP-98 auth successful!' ) ;
116+ } else {
117+ console . log ( '[Podkey] ⚠️ Retry still failed with status:' , retryResponse . status ) ;
118+ }
119+
120+ return retryResponse ;
121+ } else {
122+ console . log ( '[Podkey] No NIP-98 auth header available for retry' ) ;
105123 }
106124 } catch ( error ) {
107125 console . error ( '[Podkey] Error retrying fetch with NIP-98 auth:' , error ) ;
@@ -156,17 +174,27 @@ window.addEventListener('podkey-request', async (event) => {
156174
157175 async function getNip98AuthHeader ( url , method , body ) {
158176 try {
177+ const urlString = typeof url === 'string' ? url : url . toString ( ) ;
178+ console . log ( '[Podkey] Requesting NIP-98 auth header for:' , urlString , method ) ;
179+
159180 const response = await chrome . runtime . sendMessage ( {
160181 type : 'CREATE_NIP98_AUTH_HEADER' ,
161- url : typeof url === 'string' ? url : url . toString ( ) ,
182+ url : urlString ,
162183 method : method || 'GET' ,
163184 body : body
164185 } ) ;
165186
166187 if ( chrome . runtime . lastError ) {
188+ console . error ( '[Podkey] Error from background script:' , chrome . runtime . lastError . message ) ;
167189 return null ;
168190 }
169191
192+ if ( response ) {
193+ console . log ( '[Podkey] Got NIP-98 auth header' ) ;
194+ } else {
195+ console . log ( '[Podkey] No NIP-98 auth header (origin not trusted, auto-sign disabled, or no keypair)' ) ;
196+ }
197+
170198 return response || null ;
171199 } catch ( error ) {
172200 console . error ( '[Podkey] Error getting NIP-98 auth header:' , error ) ;
0 commit comments