fix: auth flow - logout nuclear, router redirige admin a panel, login sin bloqueo BD
This commit is contained in:
@ -36,16 +36,46 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
})
|
||||
|
||||
async function login(email: string, pass: string) {
|
||||
// Use standard Supabase signIn
|
||||
const { error } = await supabase.auth.signInWithPassword({ email, password: pass })
|
||||
const { data, error } = await supabase.auth.signInWithPassword({ email, password: pass })
|
||||
if (error) throw new Error(error.message)
|
||||
|
||||
if (data.user) {
|
||||
// Establecer sesión y rol inmediatamente desde el JWT (sin esperar BD)
|
||||
userSession.value = data.session
|
||||
// Usar user_metadata como perfil temporal para que el rol esté listo al instante
|
||||
userProfile.value = {
|
||||
id: data.user.id,
|
||||
email: data.user.email,
|
||||
full_name: data.user.user_metadata?.full_name || data.user.email,
|
||||
role: data.user.user_metadata?.role || 'PASSENGER'
|
||||
}
|
||||
// Cargar perfil completo de BD en background (sin bloquear navegación)
|
||||
supabase.from('users').select('*').eq('id', data.user.id).single()
|
||||
.then(({ data: profile }) => { if (profile) userProfile.value = profile })
|
||||
.catch(() => { })
|
||||
}
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
await supabase.auth.signOut()
|
||||
function logout() {
|
||||
// Limpiar estado de Pinia inmediatamente
|
||||
userSession.value = null
|
||||
userProfile.value = null
|
||||
window.location.href = '/'
|
||||
|
||||
// Limpiar TODA la sesión del navegador sin esperar a Supabase
|
||||
localStorage.clear()
|
||||
sessionStorage.clear()
|
||||
|
||||
// Limpiar IndexedDB donde Supabase guarda tokens
|
||||
try {
|
||||
indexedDB.deleteDatabase('supabase-js-auth')
|
||||
indexedDB.deleteDatabase('supabase')
|
||||
} catch (_) { }
|
||||
|
||||
// Llamar signOut en background (sin await - no bloqueamos)
|
||||
supabase.auth.signOut().catch(() => { })
|
||||
|
||||
// Redirigir forzando recarga completa del navegador
|
||||
window.location.replace('/login')
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user