From 632f7bbdeb957b368e9dfa9ee238edca950ad798 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 10 Dec 2024 15:36:25 +0100 Subject: [PATCH] feat: refs #7308 remove warning --- src/boot/axios.js | 12 ++++++------ src/composables/useSession.js | 14 +------------- src/router/index.js | 14 +++++++------- src/utils/session.js | 23 +++++++++++++++++++++++ 4 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 src/utils/session.js diff --git a/src/boot/axios.js b/src/boot/axios.js index aee38e887..d71928505 100644 --- a/src/boot/axios.js +++ b/src/boot/axios.js @@ -3,8 +3,8 @@ import { useSession } from 'src/composables/useSession'; import { Router } from 'src/router'; import useNotify from 'src/composables/useNotify.js'; import { useStateQueryStore } from 'src/stores/useStateQueryStore'; +import { getToken, isLoggedIn } from 'src/utils/session'; -const session = useSession(); const { notify } = useNotify(); const stateQuery = useStateQueryStore(); const baseUrl = '/api/'; @@ -13,7 +13,7 @@ axios.defaults.baseURL = baseUrl; const axiosNoError = axios.create({ baseURL: baseUrl }); const onRequest = (config) => { - const token = session.getToken(); + const token = getToken(); if (token.length && !config.headers.Authorization) { config.headers.Authorization = token; } @@ -36,15 +36,15 @@ const onResponse = (response) => { return response; }; -const onResponseError = (error) => { +const onResponseError = async (error) => { stateQuery.remove(error.config); - if (session.isLoggedIn() && error.response?.status === 401) { - session.destroy(false); + if (isLoggedIn() && error.response?.status === 401) { + await useSession().destroy(false); const hash = window.location.hash; const url = hash.slice(1); Router.push(`/login?redirect=${url}`); - } else if (!session.isLoggedIn()) { + } else if (!isLoggedIn()) { return Promise.reject(error); } diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 633a30bb0..e69819a68 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -6,6 +6,7 @@ import axios from 'axios'; import { useRouter } from 'vue-router'; import useNotify from './useNotify'; import { useTokenConfig } from './useTokenConfig'; +import { getToken, getTokenMultimedia } from 'src/utils/session'; const TOKEN_MULTIMEDIA = 'tokenMultimedia'; const TOKEN = 'token'; @@ -15,19 +16,6 @@ export function useSession() { let isCheckingToken = false; let intervalId = null; - function getToken() { - const localToken = localStorage.getItem(TOKEN); - const sessionToken = sessionStorage.getItem(TOKEN); - - return localToken || sessionToken || ''; - } - function getTokenMultimedia() { - const localTokenMultimedia = localStorage.getItem(TOKEN_MULTIMEDIA); - const sessionTokenMultimedia = sessionStorage.getItem(TOKEN_MULTIMEDIA); - - return localTokenMultimedia || sessionTokenMultimedia || ''; - } - function setSession(data) { let keepLogin = data.keepLogin; const storage = keepLogin ? localStorage : sessionStorage; diff --git a/src/router/index.js b/src/router/index.js index 18541c0b2..f2817f64d 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,4 +1,4 @@ -import { route } from 'quasar/wrappers'; +import { route as defineRouter } from 'quasar/wrappers'; import { createRouter, createMemoryHistory, @@ -8,14 +8,13 @@ import { import routes from './routes'; import { i18n } from 'src/boot/i18n'; import { useState } from 'src/composables/useState'; -import { useSession } from 'src/composables/useSession'; import { useRole } from 'src/composables/useRole'; import { useUserConfig } from 'src/composables/useUserConfig'; import { useTokenConfig } from 'src/composables/useTokenConfig'; import { useAcl } from 'src/composables/useAcl'; +import { isLoggedIn } from 'src/utils/session'; +import { useSession } from 'src/composables/useSession'; -const state = useState(); -const session = useSession(); const { t, te } = i18n.global; const createHistory = process.env.SERVER @@ -43,11 +42,12 @@ const Router = createRouter({ * with the Router instance. */ export { Router }; -export default route(function (/* { store, ssrContext } */) { +export default defineRouter(function (/* { store, ssrContext } */) { + const state = useState(); Router.beforeEach(async (to, from, next) => { - const { isLoggedIn } = session; + const session = useSession(); const outLayout = Router.options.routes[0].children.map((r) => r.name); - if (!isLoggedIn() && !outLayout.includes(to.name)) { + if (!session.isLoggedIn() && !outLayout.includes(to.name)) { return next({ name: 'Login', query: { redirect: to.fullPath } }); } diff --git a/src/utils/session.js b/src/utils/session.js new file mode 100644 index 000000000..2a8c6a744 --- /dev/null +++ b/src/utils/session.js @@ -0,0 +1,23 @@ +const TOKEN_MULTIMEDIA = 'tokenMultimedia'; +const TOKEN = 'token'; + +function getToken() { + const localToken = localStorage.getItem(TOKEN); + const sessionToken = sessionStorage.getItem(TOKEN); + + return localToken || sessionToken || ''; +} +function getTokenMultimedia() { + const localTokenMultimedia = localStorage.getItem(TOKEN_MULTIMEDIA); + const sessionTokenMultimedia = sessionStorage.getItem(TOKEN_MULTIMEDIA); + + return localTokenMultimedia || sessionTokenMultimedia || ''; +} +function isLoggedIn() { + const localToken = localStorage.getItem(TOKEN); + const sessionToken = sessionStorage.getItem(TOKEN); + + return !!(localToken || sessionToken); +} + +export { getToken, getTokenMultimedia, isLoggedIn };