Skip to content

Commit

Permalink
use a different local storage key for jwt token in admin-frontend vs …
Browse files Browse the repository at this point in the history
…frontend
  • Loading branch information
banders committed May 28, 2024
1 parent e64d6c2 commit edcfc2e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions admin-frontend/src/common/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ApiRoutes } from '../utils/constant';
import AuthService from './authService';
import { IConfigValue, IReport } from './types';

export const LOCAL_STORAGE_KEY_JWT = 'pay-transparency-admin-jwt';

export enum REPORT_FORMATS {
HTML = 'html',
PDF = 'pdf',
Expand Down Expand Up @@ -51,12 +53,12 @@ const intercept = apiAxios.interceptors.response.use(
axios.interceptors.response.eject(intercept);
return new Promise((resolve, reject) => {
AuthService.refreshAuthToken(
localStorage.getItem('jwtToken'),
localStorage.getItem(LOCAL_STORAGE_KEY_JWT),
localStorage.getItem('correlationID'),
)
.then((response) => {
if (response.jwtFrontend) {
localStorage.setItem('jwtToken', response.jwtFrontend);
localStorage.setItem(LOCAL_STORAGE_KEY_JWT, response.jwtFrontend);
localStorage.setItem('correlationID', response.correlationID);
apiAxios.defaults.headers.common['Authorization'] =
`Bearer ${response.jwtFrontend}`;
Expand All @@ -72,7 +74,7 @@ const intercept = apiAxios.interceptors.response.use(
})
.catch((e) => {
processQueue(e, null);
localStorage.removeItem('jwtToken');
localStorage.removeItem(LOCAL_STORAGE_KEY_JWT);
window.location.href = '/token-expired';
reject(new Error('token expired', { cause: e }));
});
Expand Down
8 changes: 4 additions & 4 deletions admin-frontend/src/store/modules/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ApiService from '../../common/apiService';
import ApiService, { LOCAL_STORAGE_KEY_JWT } from '../../common/apiService';
import AuthService from '../../common/authService';
import { defineStore } from 'pinia';

Expand All @@ -22,7 +22,7 @@ export const authStore = defineStore('auth', {
error: false,
isLoading: true,
loginError: false,
jwtToken: localStorage.getItem('jwtToken'),
jwtToken: localStorage.getItem(LOCAL_STORAGE_KEY_JWT),

Check failure on line 25 in admin-frontend/src/store/modules/auth.js

View workflow job for this annotation

GitHub Actions / Unit Tests (admin-frontend)

src/store/modules/__tests__/codeStore.spec.ts > CodeStore > Fetches employee range count and naics code when auth status becomes true

Error: [vitest] No "LOCAL_STORAGE_KEY_JWT" export is defined on the "../../../common/apiService" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("../../../common/apiService", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ state src/store/modules/auth.js:25:36 ❯ setup node_modules/pinia/dist/pinia.mjs:1210:49 ❯ EffectScope.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:47:16 ❯ node_modules/pinia/dist/pinia.mjs:1450:88 ❯ EffectScope.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:47:16 ❯ node_modules/pinia/dist/pinia.mjs:1450:54 ❯ runWithContext node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:3972:18 ❯ createSetupStore node_modules/pinia/dist/pinia.mjs:1450:24
}),
getters: {
acronymsGet: (state) => state.acronyms,
Expand All @@ -39,11 +39,11 @@ export const authStore = defineStore('auth', {
if (token) {
this.isAuthenticated = true;
this.jwtToken = token;
localStorage.setItem('jwtToken', token);
localStorage.setItem(LOCAL_STORAGE_KEY_JWT, token);
} else {
this.isAuthenticated = false;
this.jwtToken = null;
localStorage.removeItem('jwtToken');
localStorage.removeItem(LOCAL_STORAGE_KEY_JWT);
}
},
setCorrelationID(correlationID) {
Expand Down

0 comments on commit edcfc2e

Please sign in to comment.