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

@@ -2,7 +2,7 @@
<header class="header"> <header class="header">
<div class="container wrapper"> <div class="container wrapper">
<div class="navmenu-container"> <div class="navmenu-container">
<NavMenu @logout="logout" /> <NavMenu @handle-logout="handleLogout" />
</div> </div>
<!-- isAdmin: {{ isAdmin }} <br> <!-- isAdmin: {{ isAdmin }} <br>
isAuthenticated: {{ isAuthenticated }} <br> --> isAuthenticated: {{ isAuthenticated }} <br> -->
@@ -71,7 +71,6 @@
import { mapActions } from 'pinia' import { mapActions } from 'pinia'
import { useAuthStore } from '@/stores/auth' import { useAuthStore } from '@/stores/auth'
export default { export default {
setup() { setup() {
const auth = useAuthStore(); const auth = useAuthStore();
return { return {
@@ -89,10 +88,9 @@ export default {
return this.auth.getName return this.auth.getName
}, },
}, },
methods: { methods: {
...mapActions('auth', ['logout']), ...mapActions(useAuthStore, ['logout']),
async logout() { async handleLogout() {
try { try {
await this.logout() await this.logout()
this.$router.push('/login') this.$router.push('/login')

View File

@@ -13,12 +13,13 @@
to="/editar/productos/importar" to="/editar/productos/importar"
>Importar</NuxtLink >Importar</NuxtLink
> >
<NuxtLink to="/" @click="logout" >Cerrar sesión</NuxtLink> <NuxtLink to="/" @click="handleLogout" >Cerrar sesión</NuxtLink>
</div> </div>
</template> </template>
<script> <script>
import { useAuthStore } from '@/stores/auth' import { useAuthStore } from '@/stores/auth'
import { mapActions } from 'pinia'
export default { export default {
setup() { setup() {
const auth = useAuthStore(); const auth = useAuthStore();
@@ -35,8 +36,14 @@ export default {
await this.checkIfCoopValidated() await this.checkIfCoopValidated()
}, },
methods: { methods: {
async logout() { ...mapActions(useAuthStore, ['logout']),
await this.auth.logout() async handleLogout() {
try {
await this.logout()
this.$router.push('/')
} catch (error) {
console.error('Error logging out:', error)
}
}, },
async checkIfCoopValidated() { async checkIfCoopValidated() {
const config = useRuntimeConfig() const config = useRuntimeConfig()

View File

@@ -1,12 +1,14 @@
<template> <template>
<div class="navsearch_container container-fluid"> <div class="navsearch_container container-fluid">
<NuxtLink to="/editar/perfil">Mi perfil</NuxtLink> <NuxtLink to="/editar/perfil">Mi perfil</NuxtLink>
<NuxtLink to="/" @click="logout" >Cerrar sesión</NuxtLink> <NuxtLink to="/" @click="handleLogout" >Cerrar sesión</NuxtLink>
</div> </div>
</template> </template>
<script> <script>
import { useAuthStore } from '@/stores/auth' import { useAuthStore } from '@/stores/auth'
import { mapActions } from 'pinia'
export default { export default {
setup() { setup() {
const auth = useAuthStore(); const auth = useAuthStore();
@@ -15,8 +17,14 @@ export default {
} }
}, },
methods: { methods: {
async logout() { ...mapActions(useAuthStore, ['logout']),
await this.auth.logout() async handleLogout() {
try {
await this.logout()
this.$router.push('/')
} catch (error) {
console.error('Error logging out:', error)
}
}, },
}, },
} }

View File

@@ -95,7 +95,7 @@
<span class="section-text login">Importar</span> <span class="section-text login">Importar</span>
</li> </li>
</NuxtLink> </NuxtLink>
<NuxtLink v-if="isAuthenticated" @click="logout" to="/"> <NuxtLink v-if="isAuthenticated" @click="handleLogout" to="/">
<li class="section" @click="isMenuOpen = !isMenuOpen"> <li class="section" @click="isMenuOpen = !isMenuOpen">
<img <img
class="section-img" class="section-img"
@@ -145,6 +145,7 @@ import { mapActions } from 'pinia'
import { useAuthStore } from '@/stores/auth' import { useAuthStore } from '@/stores/auth'
export default { export default {
emits: ['handleLogout'],
setup() { setup() {
const auth = useAuthStore(); const auth = useAuthStore();
return { return {
@@ -165,11 +166,11 @@ export default {
}, },
}, },
methods: { methods: {
...mapActions('auth', ['logout']), ...mapActions(useAuthStore, ['logout']),
async logout() { async handleLogout() {
this.isMenuOpen = false this.isMenuOpen = false
this.$emit('logout') this.$emit('handleLogout')
await this.logout() //await this.logout()
}, },
}, },
} }

View File

@@ -5,20 +5,32 @@
<NuxtLink to="/admin/cooperativas"> Administrar cooperativas </NuxtLink> <NuxtLink to="/admin/cooperativas"> Administrar cooperativas </NuxtLink>
<NuxtLink to="/admin/importar"> Alta de cooperativas por .csv </NuxtLink> <NuxtLink to="/admin/importar"> Alta de cooperativas por .csv </NuxtLink>
<NuxtLink to="/admin/estadisticas"> Estadísticas </NuxtLink> <NuxtLink to="/admin/estadisticas"> Estadísticas </NuxtLink>
<NuxtLink to="/" @click="logout">Cerrar sesión</NuxtLink> <button @click="handleLogout" class="logout-link">Cerrar sesión</button>
<!-- <NuxtLink to="/" @click="handleLogout">Cerrar sesión</NuxtLink> -->
</div> </div>
<NuxtPage /> <NuxtPage />
</div> </div>
</template> </template>
<script> <script>
import { useAuthStore } from '@/stores/auth'
import { mapActions } from 'pinia' import { mapActions } from 'pinia'
export default { export default {
setup() {
const auth = useAuthStore();
return {
auth,
}
},
methods: { methods: {
...mapActions('auth', ['logout']), ...mapActions(useAuthStore, ['logout']),
async logout() { async handleLogout() {
await this.logout() try {
await this.logout()
this.$router.push('/')
} catch (error) {
console.error('Error logging out:', error)
}
}, },
}, },
} }

View File

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