From cb1542937a0345282ca4f767f9066523549bfc14 Mon Sep 17 00:00:00 2001 From: wbuezas Date: Tue, 22 Oct 2024 10:41:09 -0300 Subject: [PATCH] Token changes --- src/router/index.js | 11 +++++++++++ src/stores/user.js | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 34eb2d42..94e58388 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,5 +1,6 @@ import { route } from 'quasar/wrappers'; import { useAppStore } from 'stores/app'; +import { useUserStore } from 'stores/user'; import { createRouter, createMemoryHistory, @@ -36,6 +37,16 @@ export default route(function (/* { store, ssrContext } */) { ) }); + Router.beforeEach((to, from, next) => { + const userStore = useUserStore(); + + if (!userStore.storage.getItem('token') && to.name !== 'login') { + return next({ name: 'login' }); + } + + next(); + }); + Router.afterEach((to, from) => { if (from.name === to.name) return; const app = useAppStore(); diff --git a/src/stores/user.js b/src/stores/user.js index 8ff2751c..07ded239 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -22,16 +22,16 @@ export const useUserStore = defineStore('user', () => { const tokenConfig = ref(null); let router; - const loggedIn = computed(() => !!token.value); const storage = computed(() => keepLogin.value ? localStorage : sessionStorage ); + const isLoggedIn = computed(() => !!storage.value.getItem(TOKEN)); const init = async _router => { router = _router; isGuest.value = localStorage.getItem('hederaGuest') || false; await getToken(); - if (!loggedIn.value) { + if (!isLoggedIn.value) { const autoLoginStatus = await tryAutoLogin(); if (!autoLoginStatus) { router.push({ name: 'login' }); @@ -285,7 +285,6 @@ export const useUserStore = defineStore('user', () => { watch( [mainUser, supplantedUser], () => (user.value = supplantedUser.value || mainUser.value), - { immediate: true } ); @@ -300,7 +299,7 @@ export const useUserStore = defineStore('user', () => { intervalId, isCheckingToken, tokenConfig, - loggedIn, + isLoggedIn, storage, getToken, getTokenMultimedia,