Преглед изворни кода

fix: parse WooCommerce OAuth callback POST body correctly #18

WooCommerce sends OAuth credentials via POST request body (form-urlencoded),
not as query parameters. Updated callback handler to:
- Check request method (POST vs GET)
- Parse form-urlencoded POST body to extract credentials
- Fallback to JSON format if needed
- Support legacy GET parameter format as fallback
- Enhanced logging to show request method and credential status

This fixes the error: "An error occurred in the request and at the time
were unable to send the consumer data."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Claude пре 5 месеци
родитељ
комит
62d1edb454
1 измењених фајлова са 38 додато и 6 уклоњено
  1. 38 6
      supabase/functions/oauth-woocommerce/index.ts

+ 38 - 6
supabase/functions/oauth-woocommerce/index.ts

@@ -331,13 +331,45 @@ serve(async (req) => {
 
     // Handle OAuth callback
     if (action === 'callback') {
-      const success = url.searchParams.get('success')
-      const userId = url.searchParams.get('user_id')
-      const consumerKey = url.searchParams.get('consumer_key')
-      const consumerSecret = url.searchParams.get('consumer_secret')
-      const storeUrlParam = url.searchParams.get('store_url')
+      // WooCommerce sends OAuth credentials via POST request body
+      let success: string | null = null
+      let userId: string | null = null
+      let consumerKey: string | null = null
+      let consumerSecret: string | null = null
+      let storeUrlParam: string | null = null
+
+      // Parse POST body (WooCommerce sends form-urlencoded data)
+      if (req.method === 'POST') {
+        const contentType = req.headers.get('content-type') || ''
+
+        if (contentType.includes('application/x-www-form-urlencoded')) {
+          // Parse form-urlencoded body
+          const body = await req.text()
+          const params = new URLSearchParams(body)
+          success = params.get('success')
+          userId = params.get('user_id')
+          consumerKey = params.get('consumer_key')
+          consumerSecret = params.get('consumer_secret')
+          storeUrlParam = params.get('store_url')
+        } else if (contentType.includes('application/json')) {
+          // Fallback for JSON format
+          const body = await req.json()
+          success = body.success
+          userId = body.user_id
+          consumerKey = body.consumer_key
+          consumerSecret = body.consumer_secret
+          storeUrlParam = body.store_url
+        }
+      } else {
+        // Fallback to query parameters for GET requests
+        success = url.searchParams.get('success')
+        userId = url.searchParams.get('user_id')
+        consumerKey = url.searchParams.get('consumer_key')
+        consumerSecret = url.searchParams.get('consumer_secret')
+        storeUrlParam = url.searchParams.get('store_url')
+      }
 
-      console.log(`[WooCommerce] OAuth callback received - success: ${success}`)
+      console.log(`[WooCommerce] OAuth callback received - method: ${req.method}, success: ${success}, userId: ${userId}, hasKeys: ${!!consumerKey && !!consumerSecret}`)
 
       if (success !== '1') {
         console.error('[WooCommerce] OAuth rejected by user')