Browse Source

fix(shoprenter): fix auto-login after registration and settings API

- Fix ShopRenter settings API endpoint to use /api/settings?full=1
- Parse settings from items array with key/value structure
- Fix auto-login by setting session in Supabase client via setSession()
- Store IsAuthenticated flag in localStorage
- Fix button styling for "Register with different email" button

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

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh 4 months ago
parent
commit
ff30e4c9f0

+ 22 - 3
shopcall.ai-main/src/pages/IntegrationsRedirect.tsx

@@ -11,6 +11,7 @@ import { useTranslation } from "react-i18next";
 import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
 import { Label } from "@/components/ui/label";
 import { LoadingScreen } from "@/components/ui/loading-screen";
+import { supabase } from "@/lib/supabase";
 
 interface PendingInstallation {
   installation_id: string;
@@ -558,11 +559,29 @@ export default function IntegrationsRedirect() {
         throw new Error(data.error || 'Auto-registration failed');
       }
 
-      // Store the session for auto-login
+      // Set the session in Supabase client for auto-login
+      // This is critical - it syncs the server-side session to the client
+      if (data.session?.access_token && data.session?.refresh_token) {
+        const { error: setSessionError } = await supabase.auth.setSession({
+          access_token: data.session.access_token,
+          refresh_token: data.session.refresh_token
+        });
+
+        if (setSessionError) {
+          console.error('Failed to set session in Supabase client:', setSessionError);
+          // Continue anyway - localStorage will be set as fallback
+        } else {
+          console.log('[AutoRegister] Session set in Supabase client successfully');
+        }
+      }
+
+      // Store the session in localStorage for components that use it directly
       localStorage.setItem('session_data', JSON.stringify({
         success: true,
-        session: data.session
+        session: data.session,
+        user: data.user
       }));
+      localStorage.setItem('IsAuthenticated', 'true');
 
       // Show success toast with password reset hint
       toast({
@@ -736,7 +755,7 @@ export default function IntegrationsRedirect() {
 
             <Button
               variant="outline"
-              className="w-full border-slate-600 text-white hover:bg-slate-700"
+              className="w-full border-slate-600 text-slate-200 hover:text-white hover:bg-slate-700 bg-slate-800"
               onClick={handleSignup}
               disabled={autoRegistering}
             >

+ 14 - 30
supabase/functions/_shared/shoprenter-client.ts

@@ -281,14 +281,14 @@ export interface ShopRenterSettings {
 /**
  * Fetch shop settings directly using shopname and access token.
  * This is used during OAuth flow before a store record exists.
- * Endpoint: GET /settings?full=1&key=config_email,config_owner
+ * Endpoint: GET /settings?full=1 returns all settings with key/value in items array
  */
 export async function fetchShopSettingsDirect(
   shopname: string,
   accessToken: string
 ): Promise<ShopRenterSettings> {
   const hostname = `${shopname}.api2.myshoprenter.hu`
-  const path = '/api/settings?full=1&key=config_email,config_owner'
+  const path = '/api/settings?full=1'
 
   console.log(`[ShopRenter] Fetching shop settings for ${shopname}`)
 
@@ -308,43 +308,27 @@ export async function fetchShopSettingsDirect(
     }
 
     const data = JSON.parse(response.body)
-    console.log('[ShopRenter] Settings response:', JSON.stringify(data).substring(0, 500))
+    console.log('[ShopRenter] Settings response keys:', Object.keys(data))
+    console.log('[ShopRenter] Settings items count:', data.items?.length || 0)
 
     // Extract config_email and config_owner from the response
-    // ShopRenter settings API returns data in various formats, handle them
-    let settings: ShopRenterSettings = {}
+    // ShopRenter returns: { items: [{ id, group, key, value }, ...], ... }
+    const settings: ShopRenterSettings = {}
 
-    if (Array.isArray(data)) {
-      // If array, find the settings by key
-      for (const item of data) {
-        if (item.key === 'config_email') {
-          settings.config_email = item.value
-        } else if (item.key === 'config_owner') {
-          settings.config_owner = item.value
-        }
-      }
-    } else if (data.items && Array.isArray(data.items)) {
-      // If paginated response with items array
+    if (data.items && Array.isArray(data.items)) {
       for (const item of data.items) {
-        if (item.key === 'config_email') {
+        if (item.key === 'config_email' && item.value) {
           settings.config_email = item.value
-        } else if (item.key === 'config_owner') {
+          console.log(`[ShopRenter] Found config_email: ${item.value}`)
+        } else if (item.key === 'config_owner' && item.value) {
           settings.config_owner = item.value
+          console.log(`[ShopRenter] Found config_owner: ${item.value}`)
         }
-      }
-    } else if (data.data && Array.isArray(data.data)) {
-      // Alternative paginated format
-      for (const item of data.data) {
-        if (item.key === 'config_email') {
-          settings.config_email = item.value
-        } else if (item.key === 'config_owner') {
-          settings.config_owner = item.value
+        // Stop early if we found both
+        if (settings.config_email && settings.config_owner) {
+          break
         }
       }
-    } else if (typeof data === 'object') {
-      // Direct object format
-      settings.config_email = data.config_email || data.configEmail
-      settings.config_owner = data.config_owner || data.configOwner
     }
 
     console.log(`[ShopRenter] Extracted settings for ${shopname}:`, settings)