🚀 Feat(Core): Migración oficial y total B a Supabase (Auth, DB, APIs) + Eliminación de Backend Python y Fixes Firebase + Soporte Vite Vercel

This commit is contained in:
2026-02-25 21:01:18 -05:00
parent 87752d8214
commit 7cd97365f2
17 changed files with 868 additions and 241 deletions

View File

@ -1,39 +1,56 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { supabase } from '@/supabase'
export const useAuthStore = defineStore('auth', () => {
const token = ref<string | null>(localStorage.getItem('auth_token'))
const role = ref<string | null>(localStorage.getItem('user_role'))
const userName = ref<string | null>(localStorage.getItem('user_name'))
const userSession = ref<any>(null)
const userProfile = ref<any>(null)
const isAuthenticated = computed(() => !!userSession.value)
// We check the custom Postgres role we put in our `users` table
const role = computed(() => userProfile.value?.role || userSession.value?.user?.user_metadata?.role)
const userName = computed(() => userProfile.value?.full_name || userSession.value?.user?.user_metadata?.full_name)
const isAuthenticated = computed(() => !!token.value)
const isAdmin = computed(() => role.value?.toUpperCase() === 'ADMIN')
const isDriver = computed(() => role.value?.toUpperCase() === 'DRIVER')
const isPromoter = computed(() => role.value?.toUpperCase() === 'PROMOTER')
const isPassenger = computed(() => !role.value || role.value?.toUpperCase() === 'PASSENGER')
function login(newToken: string, newRole: string, newName: string) {
token.value = newToken
role.value = newRole
userName.value = newName
localStorage.setItem('auth_token', newToken)
localStorage.setItem('user_role', newRole)
localStorage.setItem('user_name', newName)
/** Listens for Supabase Auth state changes */
supabase.auth.onAuthStateChange(async (_event, session) => {
userSession.value = session
if (session?.user) {
// Immediately fetch their role from standard Public table if present
const { data } = await supabase
.from('users')
.select('*')
.eq('id', session.user.id)
.single()
if (data) userProfile.value = data
} else {
userProfile.value = null
}
})
async function login(email: string, pass: string) {
// Use standard Supabase signIn
const { error } = await supabase.auth.signInWithPassword({ email, password: pass })
if (error) throw new Error(error.message)
}
function logout() {
token.value = null
role.value = null
userName.value = null
localStorage.removeItem('auth_token')
localStorage.removeItem('user_role')
localStorage.removeItem('user_name')
async function logout() {
await supabase.auth.signOut()
userSession.value = null
userProfile.value = null
window.location.href = '/'
}
return {
token,
userSession,
userProfile,
role,
userName,
isAuthenticated,