import { useState } from 'react'; import apiClient from '../api/client'; import { setCookie } from '../utils/cookies'; import { useQueryClient } from '@tanstack/react-query'; import { getBaseDomain, buildSubdomainUrl } from '../utils/domain'; export interface TestUser { email: string; password: string; role: string; label: string; color: string; } const testUsers: TestUser[] = [ { email: 'superuser@platform.com', password: 'test123', role: 'SUPERUSER', label: 'Platform Superuser', color: 'bg-purple-600 hover:bg-purple-700', }, { email: 'manager@platform.com', password: 'test123', role: 'PLATFORM_MANAGER', label: 'Platform Manager', color: 'bg-blue-600 hover:bg-blue-700', }, { email: 'sales@platform.com', password: 'test123', role: 'PLATFORM_SALES', label: 'Platform Sales', color: 'bg-green-600 hover:bg-green-700', }, { email: 'support@platform.com', password: 'test123', role: 'PLATFORM_SUPPORT', label: 'Platform Support', color: 'bg-yellow-600 hover:bg-yellow-700', }, { email: 'owner@demo.com', password: 'test123', role: 'TENANT_OWNER', label: 'Business Owner', color: 'bg-indigo-600 hover:bg-indigo-700', }, { email: 'manager@demo.com', password: 'test123', role: 'TENANT_MANAGER', label: 'Business Manager', color: 'bg-pink-600 hover:bg-pink-700', }, { email: 'staff@demo.com', password: 'test123', role: 'TENANT_STAFF', label: 'Staff Member', color: 'bg-teal-600 hover:bg-teal-700', }, { email: 'customer@demo.com', password: 'test123', role: 'CUSTOMER', label: 'Customer', color: 'bg-orange-600 hover:bg-orange-700', }, ]; interface DevQuickLoginProps { embedded?: boolean; } export function DevQuickLogin({ embedded = false }: DevQuickLoginProps) { const queryClient = useQueryClient(); const [loading, setLoading] = useState(null); const [isMinimized, setIsMinimized] = useState(false); // Only show in development if (import.meta.env.PROD) { return null; } const handleQuickLogin = async (user: TestUser) => { setLoading(user.email); try { // Call custom login API that supports email login const response = await apiClient.post('/auth/login/', { email: user.email, password: user.password, }); // Store token in cookie (use 'access_token' to match what client.ts expects) setCookie('access_token', response.data.access, 7); // Clear any existing masquerade stack - this is a fresh login localStorage.removeItem('masquerade_stack'); // Fetch user data to determine redirect const userResponse = await apiClient.get('/auth/me/'); const userData = userResponse.data; // Determine the correct subdomain based on user role const currentHostname = window.location.hostname; const currentPort = window.location.port; let targetSubdomain: string | null = null; // Platform users (superuser, platform_manager, platform_support) if (['superuser', 'platform_manager', 'platform_support'].includes(userData.role)) { targetSubdomain = 'platform'; } // Business users - redirect to their business subdomain else if (userData.business_subdomain) { targetSubdomain = userData.business_subdomain; } // Check if we need to redirect to a different subdomain const baseDomain = getBaseDomain(); const isOnTargetSubdomain = currentHostname === (targetSubdomain ? `${targetSubdomain}.${baseDomain}` : baseDomain); const needsRedirect = targetSubdomain && !isOnTargetSubdomain; if (needsRedirect) { // Redirect to the correct subdomain window.location.href = buildSubdomainUrl(targetSubdomain, '/'); return; } // Already on correct subdomain - just reload to update auth state window.location.reload(); } catch (error: any) { console.error('Quick login failed:', error); alert(`Failed to login as ${user.label}: ${error.message || 'Unknown error'}`); } finally { setLoading(null); } }; if (!embedded && isMinimized) { return (
); } const containerClasses = embedded ? "w-full bg-gray-50 dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4 mt-6" : "fixed bottom-4 right-4 z-50 bg-white rounded-lg shadow-2xl border-2 border-gray-300 p-4 max-w-md"; return (

🔓 Quick Login (Dev Only)

{!embedded && ( )}
{testUsers.map((user) => ( ))}
Password for all: test123
); }