From 974e58d488e0f0c0ac5a3e2f189cbe01b85cc4f5 Mon Sep 17 00:00:00 2001 From: Quentin Guillemin Date: Thu, 18 Jan 2024 15:16:35 +0100 Subject: [PATCH] refactor: store user sub into store --- src/main/webapp/src/stores/configurationStore.ts | 8 +++++++- src/main/webapp/src/types/soffitType.ts | 3 +++ src/main/webapp/src/utils/axiosUtils.ts | 8 ++++---- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/main/webapp/src/types/soffitType.ts diff --git a/src/main/webapp/src/stores/configurationStore.ts b/src/main/webapp/src/stores/configurationStore.ts index bf81c4f1..cc7b4aff 100644 --- a/src/main/webapp/src/stores/configurationStore.ts +++ b/src/main/webapp/src/stores/configurationStore.ts @@ -4,6 +4,7 @@ import type { Configuration } from '@/types/configurationType.ts'; import { Navigation } from '@/types/enums/Navigation.ts'; import { Tabs } from '@/types/enums/Tabs.ts'; import type { File } from '@/types/fileType.ts'; +import type { Soffit } from '@/types/soffitType.ts'; import { errorHandler } from '@/utils/axiosUtils.ts'; import { differenceInMilliseconds } from 'date-fns'; import debounce from 'lodash.debounce'; @@ -29,7 +30,11 @@ export const useConfigurationStore = defineStore('configuration', () => { const isInit = computed(() => configuration.value != undefined); - const isSoffitOk = ref(false); + const user = ref({ + sub: 'guest', + }); + + const isSoffitOk = computed(() => !user.value.sub.startsWith('guest')); /* -- Gestion de la navigation -- */ @@ -117,6 +122,7 @@ export const useConfigurationStore = defineStore('configuration', () => { return { init, isInit, + user, isSoffitOk, lastNavigation, search, diff --git a/src/main/webapp/src/types/soffitType.ts b/src/main/webapp/src/types/soffitType.ts new file mode 100644 index 00000000..5c4ed086 --- /dev/null +++ b/src/main/webapp/src/types/soffitType.ts @@ -0,0 +1,3 @@ +export type Soffit = { + sub: string; +}; diff --git a/src/main/webapp/src/utils/axiosUtils.ts b/src/main/webapp/src/utils/axiosUtils.ts index 1e3d0354..0f4e6f8d 100644 --- a/src/main/webapp/src/utils/axiosUtils.ts +++ b/src/main/webapp/src/utils/axiosUtils.ts @@ -1,5 +1,5 @@ import i18n from '@/plugins/i18n.ts'; -import { useConfigurationStore } from '@/stores/configurationStore'; +import { useConfigurationStore } from '@/stores/configurationStore.ts'; import { getToken } from '@/utils/soffitUtils.ts'; import axios from 'axios'; import throttle from 'lodash.throttle'; @@ -22,7 +22,7 @@ let renewToken: any; const init = async (): Promise => { const configurationStore = useConfigurationStore(); - const { isSoffitOk } = storeToRefs(configurationStore); + const { user } = storeToRefs(configurationStore); try { const { @@ -39,7 +39,7 @@ const init = async (): Promise => { decoded: { sub }, } = await getToken(); token = `Bearer ${encoded}`; - isSoffitOk.value = !sub.startsWith('guest'); + user.value = { ...user.value, sub }; } catch (e) { // nothing to do } @@ -47,7 +47,7 @@ const init = async (): Promise => { timeout, { trailing: false }, ); - isSoffitOk.value = !sub.startsWith('guest'); + user.value = { ...user.value, sub }; } catch (e) { // nothing to do }