Browse Source

fix: preserve ShopRenter client credentials in alt_data during token refresh #83

- Fixed bug where client_id and client_secret were not being saved to alt_data during token refresh operations
- Updated refresh_token flow (lines 376-389) to preserve credentials in alt_data
- Updated fallback client_credentials flow (lines 401-421) to preserve credentials in alt_data
- Ensures consistent backup of OAuth credentials across all token refresh scenarios
- Prevents loss of client credentials if api_key/api_secret fields get corrupted
Claude 5 months ago
parent
commit
94e4731241
1 changed files with 15 additions and 2 deletions
  1. 15 2
      supabase/functions/_shared/shoprenter-client.ts

+ 15 - 2
supabase/functions/_shared/shoprenter-client.ts

@@ -373,12 +373,18 @@ export async function getValidAccessToken(storeId: string): Promise<string> {
           const newExpiresAt = new Date(Date.now() + (newTokenData.expires_in * 1000)).toISOString()
           const newExpiresAt = new Date(Date.now() + (newTokenData.expires_in * 1000)).toISOString()
 
 
           // Update store with new tokens
           // Update store with new tokens
+          // IMPORTANT: Also preserve client credentials in alt_data for backup
           await supabase
           await supabase
             .from('stores')
             .from('stores')
             .update({
             .update({
               access_token: newTokenData.access_token,
               access_token: newTokenData.access_token,
               refresh_token: newTokenData.refresh_token || store.refresh_token,
               refresh_token: newTokenData.refresh_token || store.refresh_token,
-              token_expires_at: newExpiresAt
+              token_expires_at: newExpiresAt,
+              alt_data: {
+                ...store.alt_data,
+                client_id: clientId,
+                client_secret: clientSecret
+              }
             })
             })
             .eq('id', storeId)
             .eq('id', storeId)
 
 
@@ -398,12 +404,19 @@ export async function getValidAccessToken(storeId: string): Promise<string> {
 
 
             const expiresAt = new Date(Date.now() + (tokenData.expires_in * 1000)).toISOString()
             const expiresAt = new Date(Date.now() + (tokenData.expires_in * 1000)).toISOString()
 
 
+            // Update store with new tokens
+            // IMPORTANT: Also preserve client credentials in alt_data for backup
             await supabase
             await supabase
               .from('stores')
               .from('stores')
               .update({
               .update({
                 access_token: tokenData.access_token,
                 access_token: tokenData.access_token,
                 refresh_token: tokenData.refresh_token || null,
                 refresh_token: tokenData.refresh_token || null,
-                token_expires_at: expiresAt
+                token_expires_at: expiresAt,
+                alt_data: {
+                  ...store.alt_data,
+                  client_id: clientId,
+                  client_secret: clientSecret
+                }
               })
               })
               .eq('id', storeId)
               .eq('id', storeId)