From eb4dc6bf17cffdaa6ccbaf11b4b2e681568d9f69 Mon Sep 17 00:00:00 2001 From: Severin Beauvais Date: Fri, 6 Oct 2023 10:36:33 -0700 Subject: [PATCH] - added isAuthenticated as state property - set isAuthenticated when Keycloak roles are loaded (call by App.vue and Signin.vue) - added setIsAuthenticated action - updated isAuthenticated getter - added mutateIsAuthenticated mutation - initialized isAuthenticated state --- src/interfaces/state-interface.ts | 3 ++- src/mixins/load-keycloak-roles-mixin.ts | 5 +++-- src/store/actions.ts | 5 ++++- src/store/getters.ts | 12 +++++------- src/store/mutations.ts | 4 ++++ src/store/state.ts | 3 ++- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/interfaces/state-interface.ts b/src/interfaces/state-interface.ts index 314218c77..01356646d 100644 --- a/src/interfaces/state-interface.ts +++ b/src/interfaces/state-interface.ts @@ -3,8 +3,9 @@ import { NameCheckModelIF, StaffPaymentIF, RefundParamsIF } from '@/interfaces' export interface StateModelIF { common: { - currentJsDate: Date, + currentJsDate: Date keycloakRoles: Array + isAuthenticated: boolean } newRequestModel: NewRequestIF staffPayment: StaffPaymentIF diff --git a/src/mixins/load-keycloak-roles-mixin.ts b/src/mixins/load-keycloak-roles-mixin.ts index c7fab31cc..839ef053b 100644 --- a/src/mixins/load-keycloak-roles-mixin.ts +++ b/src/mixins/load-keycloak-roles-mixin.ts @@ -1,16 +1,17 @@ import { Component, Vue } from 'vue-property-decorator' import { getKeycloakRoles } from '@/plugins' import { Action } from 'vuex-class' -import { ActionBindingIF } from '@/interfaces/store-interfaces' @Component({}) export class LoadKeycloakRolesMixin extends Vue { - @Action setKeycloakRoles!: ActionBindingIF + @Action setKeycloakRoles!: (val: string[]) => void + @Action setIsAuthenticated!: (val: boolean) => void /** Gets and stores Keycloak roles. */ loadKeycloakRoles (): void { try { const keycloakRoles = getKeycloakRoles() + this.setIsAuthenticated(keycloakRoles.length > 0) this.setKeycloakRoles(keycloakRoles) console.info('Got roles!') // eslint-disable-line no-console } catch (err) { diff --git a/src/store/actions.ts b/src/store/actions.ts index 8ea908fa7..0f0acd48d 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -2,7 +2,6 @@ import querystring from 'qs' import axios from 'axios' import { CompanyTypes, - EntityStates, EntityTypes, Location, NameCheckAnalysisJurisdiction, @@ -720,6 +719,10 @@ export const setRequestExaminationOrProvideConsent = ({ commit }, requestExamOrC commit('mutateRequestExaminationOrProvideConsent', requestExamOrConsent) } +export const setIsAuthenticated = ({ commit }, val: boolean): void => { + commit('mutateIsAuthenticated', val) +} + export const setKeycloakRoles = ({ commit }, keycloakRoles: string[]): void => { commit('mutateKeycloakRoles', keycloakRoles) } diff --git a/src/store/getters.ts b/src/store/getters.ts index 43fa6ff6b..547030ca5 100644 --- a/src/store/getters.ts +++ b/src/store/getters.ts @@ -16,7 +16,6 @@ import { NameDesignationI, NameRequestI, RefundParamsIF, - RequestActionMappingI, RequestActionsI, RequestNameI, RequestOrConsentIF, @@ -38,7 +37,6 @@ import { PriorityCode, XproNameType } from '@/enums' -import { SessionStorageKeys } from 'sbc-common-components/src/util/constants' // List Data // NB: can't use `this.$xxx` because we don't have `this` (ie, Vue) @@ -66,11 +64,6 @@ export const isMobile = (state: StateIF): boolean => { return (width < vuetifySm) } -/** True if user is authenticated, else False. */ -export const isAuthenticated = (): boolean => { - return Boolean(sessionStorage.getItem(SessionStorageKeys.KeyCloakToken)) -} - export const getCurrentJsDate = (state: StateIF): Date => { return state.stateModel.common.currentJsDate } @@ -1124,6 +1117,11 @@ export const getConditionalNameReservation = (state: StateIF): ConditionalReqI = return data } +/** Whether user is authenticated. */ +export const isAuthenticated = (state: StateIF): boolean => { + return state.stateModel.common.isAuthenticated +} + /** The user's keycloak roles. */ export const getKeycloakRoles = (state: StateIF): Array => { return state.stateModel.common.keycloakRoles diff --git a/src/store/mutations.ts b/src/store/mutations.ts index 9ab8c3864..25a258a6b 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -454,6 +454,10 @@ export const mutateUserCancelledAnalysis = (state: StateIF, userCancelledAnalysi state.stateModel.newRequestModel.userCancelledAnalysis = userCancelledAnalysis } +export const mutateIsAuthenticated = (state: StateIF, val: boolean) => { + state.stateModel.common.isAuthenticated = val +} + export const mutateKeycloakRoles = (state: StateIF, keyCloakRoles: Array) => { state.stateModel.common.keycloakRoles = keyCloakRoles } diff --git a/src/store/state.ts b/src/store/state.ts index cd123797e..6dffbf4e9 100644 --- a/src/store/state.ts +++ b/src/store/state.ts @@ -10,7 +10,8 @@ import { export const stateModel: StateModelIF = { common: { currentJsDate: null, - keycloakRoles: [] + keycloakRoles: [], + isAuthenticated: false }, newRequestModel: { actingOnOwnBehalf: false,