Implement Session Persistence: 'Keep me logged in' now works by toggling between localStorage and sessionStorage based on user choice

This commit is contained in:
2026-03-01 12:19:30 -05:00
parent 4d7b472c6c
commit a092df33f7
4 changed files with 122 additions and 50 deletions

View File

@ -35,12 +35,22 @@ export const useAuthStore = defineStore('auth', () => {
}
})
async function login(email: string, pass: string) {
async function login(email: string, pass: string, keepSession: boolean = false) {
const { data, error } = await supabase.auth.signInWithPassword({ email, password: pass })
if (error) throw new Error(error.message)
if (data.user) {
// Rol disponible al instante desde el JWT — sin consultas BD bloqueantes
// Manejo de persistencia: Si el usuario NO quiere mantener sesión iniciada,
// movemos el token de localStorage a sessionStorage.
if (!keepSession) {
const storageKey = `sb-bjgixlugjzsccazdfmph-auth-token`
const sessionData = localStorage.getItem(storageKey)
if (sessionData) {
sessionStorage.setItem(storageKey, sessionData)
localStorage.removeItem(storageKey)
}
}
userSession.value = data.session
userProfile.value = {
id: data.user.id,