This commit is contained in:
@@ -4,7 +4,7 @@ import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetFooter } from '@/com
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { SUPABASE_URL, SUPABASE_ANON_KEY } from '@/lib/supabase'
|
||||
import { supabase } from '@/lib/supabase'
|
||||
import { useAuth } from '@/auth/AuthContext'
|
||||
|
||||
interface ProfileSheetProps {
|
||||
@@ -12,22 +12,6 @@ interface ProfileSheetProps {
|
||||
onOpenChange: (open: boolean) => void
|
||||
}
|
||||
|
||||
// Lee el access token de localStorage sin pasar por el processLock de Supabase.
|
||||
// El token es escrito por GoTrueClient tras cada login/refresh y siempre está vigente
|
||||
// mientras la sesión esté activa.
|
||||
function getAccessToken(): string | null {
|
||||
try {
|
||||
const key = Object.keys(localStorage).find(
|
||||
(k) => k.startsWith('sb-') && k.endsWith('-auth-token')
|
||||
)
|
||||
if (!key) return null
|
||||
const stored = JSON.parse(localStorage.getItem(key) ?? 'null') as Record<string, unknown> | null
|
||||
return (stored?.access_token as string | undefined) ?? null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function ProfileSheet({ open, onOpenChange }: ProfileSheetProps) {
|
||||
const { user, updateUser } = useAuth()
|
||||
const [name, setName] = useState('')
|
||||
@@ -57,30 +41,16 @@ export function ProfileSheet({ open, onOpenChange }: ProfileSheetProps) {
|
||||
onOpenChange(false)
|
||||
|
||||
// Persistir en Supabase en background — fire & forget.
|
||||
// Si falla (red lenta, token vencido, cold start de Supabase), el estado local
|
||||
// ya tiene el nuevo nombre. En el próximo login, syncProfileFromDB recupera el DB.
|
||||
const token = getAccessToken()
|
||||
if (!token) return
|
||||
|
||||
void fetch(
|
||||
`${SUPABASE_URL}/rest/v1/users?id=eq.${encodeURIComponent(user.id)}`,
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
apikey: SUPABASE_ANON_KEY,
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
Prefer: 'return=minimal',
|
||||
},
|
||||
body: JSON.stringify({ name: savedName, company: savedCompany ?? null }),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
}
|
||||
)
|
||||
.then((res) => {
|
||||
if (!res.ok) console.error('[ProfileSheet] Error guardando perfil:', res.status)
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[ProfileSheet] Error guardando perfil:', err)
|
||||
// Usamos el cliente Supabase (no fetch nativo) para que maneje el refresh de token
|
||||
// automáticamente. Como el Sheet ya cerró, processLock puede tardar lo que necesite
|
||||
// sin bloquear el UI. lockAcquireTimeout: 30s en supabase.ts garantiza que
|
||||
// eventualmente libera si hay contención (no queda colgado para siempre).
|
||||
void supabase
|
||||
.from('users')
|
||||
.update({ name: savedName, company: savedCompany ?? null })
|
||||
.eq('id', user.id)
|
||||
.then(({ error }) => {
|
||||
if (error) console.error('[ProfileSheet] Error guardando perfil:', error)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user