This commit is contained in:
@@ -4,7 +4,7 @@ import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetFooter } from '@/com
|
|||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { Button } from '@/components/ui/button'
|
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'
|
import { useAuth } from '@/auth/AuthContext'
|
||||||
|
|
||||||
interface ProfileSheetProps {
|
interface ProfileSheetProps {
|
||||||
@@ -12,22 +12,6 @@ interface ProfileSheetProps {
|
|||||||
onOpenChange: (open: boolean) => void
|
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) {
|
export function ProfileSheet({ open, onOpenChange }: ProfileSheetProps) {
|
||||||
const { user, updateUser } = useAuth()
|
const { user, updateUser } = useAuth()
|
||||||
const [name, setName] = useState('')
|
const [name, setName] = useState('')
|
||||||
@@ -57,30 +41,16 @@ export function ProfileSheet({ open, onOpenChange }: ProfileSheetProps) {
|
|||||||
onOpenChange(false)
|
onOpenChange(false)
|
||||||
|
|
||||||
// Persistir en Supabase en background — fire & forget.
|
// Persistir en Supabase en background — fire & forget.
|
||||||
// Si falla (red lenta, token vencido, cold start de Supabase), el estado local
|
// Usamos el cliente Supabase (no fetch nativo) para que maneje el refresh de token
|
||||||
// ya tiene el nuevo nombre. En el próximo login, syncProfileFromDB recupera el DB.
|
// automáticamente. Como el Sheet ya cerró, processLock puede tardar lo que necesite
|
||||||
const token = getAccessToken()
|
// sin bloquear el UI. lockAcquireTimeout: 30s en supabase.ts garantiza que
|
||||||
if (!token) return
|
// eventualmente libera si hay contención (no queda colgado para siempre).
|
||||||
|
void supabase
|
||||||
void fetch(
|
.from('users')
|
||||||
`${SUPABASE_URL}/rest/v1/users?id=eq.${encodeURIComponent(user.id)}`,
|
.update({ name: savedName, company: savedCompany ?? null })
|
||||||
{
|
.eq('id', user.id)
|
||||||
method: 'PATCH',
|
.then(({ error }) => {
|
||||||
headers: {
|
if (error) console.error('[ProfileSheet] Error guardando perfil:', error)
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user