fix: evita logout al cambiar de tenant (race condition refresh token)

This commit is contained in:
Horux Dev
2026-06-22 21:47:13 +00:00
parent 3c7758a599
commit cc002adbd2
4 changed files with 80 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ import { useState, useEffect } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { useAuthStore } from '@/stores/auth-store';
import { switchTenant } from '@/lib/api/auth';
import { cancelAllApiRequests } from '@/lib/api/client';
import { Building2, ChevronDown, Check, Loader2, Crown } from 'lucide-react';
import { cn } from '@horux/shared-ui';
import { isGlobalAdminRfc } from '@horux/shared';
@@ -44,6 +45,9 @@ export function MembershipSwitcher() {
const handleSwitch = async (tenantId: string) => {
if (tenantId === user?.tenantId) { setOpen(false); return; }
setSwitching(true);
// Cancela requests pendientes para evitar que intenten refrescar con el
// token que switchTenant va a invalidar.
cancelAllApiRequests();
try {
const res = await switchTenant(tenantId);
setTokens(res.accessToken, res.refreshToken);