Implement Session Persistence: 'Keep me logged in' now works by toggling between localStorage and sessionStorage based on user choice
This commit is contained in:
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user