fix logout action

This commit is contained in:
María
2025-08-27 10:55:25 +02:00
parent a9a525d858
commit 5577628c56
6 changed files with 59 additions and 32 deletions

View File

@@ -4,14 +4,15 @@ import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
export const useAuthStore = defineStore('auth', {
state: () => ({
access: null as string | null,
refresh: null as string | null,
refreshTokens: null as string | null,
id: null as number | null,
name: null as string | null,
email: null as string | null,
role: 'ANON' as string,
cookiesAreAccepted: false,
}),
//persist: true, // TODO: Enable persistence. Cookies will be stored 'auth' 👉🏻 https://prazdevs.github.io/pinia-plugin-persistedstate/frameworks/nuxt
persist: true, // TODO: Enable persistence. Cookies will be stored 'auth' 👉🏻 https://prazdevs.github.io/pinia-plugin-persistedstate/frameworks/nuxt
// persist: {
// key: 'authentication-cookie',
// storage: piniaPluginPersistedstate.cookies({
@@ -25,7 +26,7 @@ export const useAuthStore = defineStore('auth', {
// 'email',
// 'role',
// 'access',
// 'refresh',
// 'refreshTokens',
// 'cookiesAreAccepted',
// ],
// },
@@ -68,22 +69,22 @@ export const useAuthStore = defineStore('auth', {
}
},
async refresh() {
async refreshAccessToken() {
const config = useRuntimeConfig()
if (!this.refresh) return
if (!this.refreshTokens) return
const data = await $fetch('/token/refresh/', {
baseURL: config.public.baseURL,
method: 'POST',
body: { refresh: this.refresh }
body: { refresh: this.refreshTokens }
})
this.setPayload(data)
},
// Mutations migration
logout() {
this.$reset() // Reset the store state
async logout() {
this.$reset()
},
// Mutations migration
acceptCookies() {
this.cookiesAreAccepted = true
},
@@ -98,7 +99,7 @@ export const useAuthStore = defineStore('auth', {
setPayload(payload: any) {
this.access = payload.access
if (payload.refresh) {
this.refresh = payload.refresh
this.refreshTokens = payload.refresh
}
}
}