Skip to content

Commit

Permalink
refactor: store user sub into store
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Jan 18, 2024
1 parent a4c5602 commit 974e58d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/main/webapp/src/stores/configurationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -29,7 +30,11 @@ export const useConfigurationStore = defineStore('configuration', () => {

const isInit = computed<boolean>(() => configuration.value != undefined);

const isSoffitOk = ref<boolean>(false);
const user = ref<Soffit>({
sub: 'guest',
});

const isSoffitOk = computed<boolean>(() => !user.value.sub.startsWith('guest'));

/* -- Gestion de la navigation -- */

Expand Down Expand Up @@ -117,6 +122,7 @@ export const useConfigurationStore = defineStore('configuration', () => {
return {
init,
isInit,
user,
isSoffitOk,
lastNavigation,
search,
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/src/types/soffitType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Soffit = {
sub: string;
};
8 changes: 4 additions & 4 deletions src/main/webapp/src/utils/axiosUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,7 +22,7 @@ let renewToken: any;

const init = async (): Promise<void> => {
const configurationStore = useConfigurationStore();
const { isSoffitOk } = storeToRefs(configurationStore);
const { user } = storeToRefs(configurationStore);

try {
const {
Expand All @@ -39,15 +39,15 @@ const init = async (): Promise<void> => {
decoded: { sub },
} = await getToken();
token = `Bearer ${encoded}`;
isSoffitOk.value = !sub.startsWith('guest');
user.value = { ...user.value, sub };
} catch (e) {
// nothing to do
}
},
timeout,
{ trailing: false },
);
isSoffitOk.value = !sub.startsWith('guest');
user.value = { ...user.value, sub };
} catch (e) {
// nothing to do
}
Expand Down

0 comments on commit 974e58d

Please sign in to comment.