Merge branch 'dev' into 7103-testVnSearchbar
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
PAU ROVIRA ROSALENY 2025-01-22 10:43:41 +00:00
commit ffe17751d9
5 changed files with 39 additions and 27 deletions

View File

@ -20,7 +20,7 @@ describe('Axios boot', () => {
describe('onRequest()', async () => {
it('should set the "Authorization" property on the headers', async () => {
const config = { headers: {} };
localStorage.setItem('token', 'DEFAULT_TOKEN');
const resultConfig = onRequest(config);
expect(resultConfig).toEqual(

View File

@ -3,9 +3,9 @@ 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';
import { i18n } from 'src/boot/i18n';
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;
config.headers['Accept-Language'] = i18n.global.locale.value;
@ -37,15 +37,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);
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
import { route } from 'quasar/wrappers';
import { route as defineRouter } from 'quasar/wrappers';
import {
createRouter,
createMemoryHistory,
@ -8,14 +8,14 @@ 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();
let session = null;
const { t, te } = i18n.global;
const createHistory = process.env.SERVER
@ -43,11 +43,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;
if (!session) 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 } });
}

23
src/utils/session.js Normal file
View File

@ -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 };