Skip to content

Commit

Permalink
Local storage handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mjturt committed Aug 22, 2023
1 parent 188747f commit f013b0b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 30 deletions.
7 changes: 3 additions & 4 deletions frontend/benefit/applicant/src/hooks/useUserQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useQuery, UseQueryResult } from 'react-query';
import showErrorToast from 'shared/components/toast/show-error-toast';
import useBackendAPI from 'shared/hooks/useBackendAPI';
import useLocale from 'shared/hooks/useLocale';
import { setLocalStorageItem } from 'shared/utils/localstorage.utils';

import { LOCAL_STORAGE_KEYS } from '../constants';

Expand Down Expand Up @@ -51,14 +52,12 @@ const useUserQuery = (
select: (data) => camelcaseKeys(data, { deep: true }),
onError: (error) => handleError(error),
onSuccess: (data) => {
/* eslint-disable scanjs-rules/identifier_localStorage */
localStorage.setItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN, data.csrfToken as string);
setLocalStorageItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN, data.csrfToken);
if (data.id && data.termsOfServiceApprovalNeeded)
localStorage.setItem(
setLocalStorageItem(
LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED,
'false'
);
/* eslint-enable scanjs-rules/identifier_localStorage */
},
}
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/benefit/applicant/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const App: React.FC<AppProps> = (appProps) => {
<BackendAPIProvider
baseURL={getBackendDomain()}
headers={getHeaders(locale)}
useLocalStorageCsrf
isLocalStorageCsrf
>
<QueryClientProvider client={queryClient}>
<AuthProvider>
Expand Down
11 changes: 4 additions & 7 deletions frontend/benefit/applicant/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import {
$GridCell,
} from 'shared/components/forms/section/FormSection.sc';
import getServerSideTranslations from 'shared/i18n/get-server-side-translations';
import { removeLocalStorageItem } from 'shared/utils/localstorage.utils';

import { IS_CLIENT, LOCAL_STORAGE_KEYS } from '../constants';
import { LOCAL_STORAGE_KEYS } from '../constants';

type NotificationProps =
| (Pick<HDSNotificationProps, 'type' | 'label'> & {
Expand Down Expand Up @@ -60,12 +61,8 @@ const Login: NextPage = () => {
}, [logout, queryClient]);

useEffect(() => {
if (IS_CLIENT) {
/* eslint-disable scanjs-rules/identifier_localStorage */
localStorage.removeItem(LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED);
localStorage.removeItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN);
/* eslint-enable scanjs-rules/identifier_localStorage */
}
removeLocalStorageItem(LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED);
removeLocalStorageItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN);
}, []);

return (
Expand Down
4 changes: 2 additions & 2 deletions frontend/benefit/handler/src/hooks/useUserQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useQuery, UseQueryResult } from 'react-query';
import useBackendAPI from 'shared/hooks/useBackendAPI';
import useLocale from 'shared/hooks/useLocale';
import User from 'shared/types/user';
import { setLocalStorageItem } from 'shared/utils/localstorage.utils';

import { LOCAL_STORAGE_KEYS, ROUTES } from '../constants';
import useLogout from './useLogout';
Expand Down Expand Up @@ -48,8 +49,7 @@ const useUserQuery = <T extends User>(

const onSuccessHandler = (user: User): void => {
checkForStaffStatus(user);
// eslint-disable-next-line scanjs-rules/identifier_localStorage
localStorage.setItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN, user.csrf_token);
setLocalStorageItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN, user.csrf_token);
};

return useQuery(
Expand Down
2 changes: 1 addition & 1 deletion frontend/benefit/handler/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const App: React.FC<AppProps> = (appProps) => {
<BackendAPIProvider
baseURL={getBackendDomain()}
headers={getHeaders(locale)}
useLocalStorageCsrf
isLocalStorageCsrf
>
<AppContextProvider>
<QueryClientProvider client={queryClient}>
Expand Down
5 changes: 2 additions & 3 deletions frontend/benefit/handler/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, { useEffect } from 'react';
import { useQueryClient } from 'react-query';
import Container from 'shared/components/container/Container';
import getServerSideTranslations from 'shared/i18n/get-server-side-translations';
import { removeLocalStorageItem } from 'shared/utils/localstorage.utils';
import { useTheme } from 'styled-components';

import { LOCAL_STORAGE_KEYS } from '../constants';
Expand Down Expand Up @@ -64,9 +65,7 @@ const Login: NextPage = () => {
}, [router.query.logout, router.query.userStateError, queryClient]);

useEffect(() => {
if (typeof window !== 'undefined')
// eslint-disable-next-line scanjs-rules/identifier_localStorage
localStorage.removeItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN);
removeLocalStorageItem(LOCAL_STORAGE_KEYS.CSRF_TOKEN);
}, []);

return (
Expand Down
18 changes: 6 additions & 12 deletions frontend/shared/src/backend-api/BackendAPIProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@ import Axios from 'axios';
import React from 'react';
import { getLastCookieValue } from 'shared/cookies/get-last-cookie-value';
import { Headers } from 'shared/types/common';
import { getLocalStorageItem } from 'shared/utils/localstorage.utils';

import BackendAPIContext from './BackendAPIContext';

export interface BackendAPIProviderProps {
baseURL: string;
headers?: Headers;
useLocalStorageCsrf?: boolean;
isLocalStorageCsrf?: boolean;
}

const getCsrfToken = (useLocalStorageCsrf: boolean): string => {
if (useLocalStorageCsrf)
return typeof window !== 'undefined'
? // eslint-disable-next-line scanjs-rules/identifier_localStorage
localStorage.getItem('csrfToken') || ''
: '';
return getLastCookieValue('yjdhcsrftoken');
};

const BackendAPIProvider: React.FC<BackendAPIProviderProps> = ({
baseURL,
headers,
useLocalStorageCsrf = false,
isLocalStorageCsrf = false,
children,
}): JSX.Element => (
<BackendAPIContext.Provider
value={Axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': getCsrfToken(useLocalStorageCsrf),
'X-CSRFToken': isLocalStorageCsrf
? getLocalStorageItem('csrfToken')
: getLastCookieValue('yjdhcsrftoken'),
...headers,
},
withCredentials: true,
Expand Down
14 changes: 14 additions & 0 deletions frontend/shared/src/utils/localstorage.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable scanjs-rules/identifier_localStorage */

const IS_CLIENT = typeof window !== 'undefined';

export const getLocalStorageItem = (key: string): string =>
IS_CLIENT ? localStorage.getItem(key) || '' : '';

export const setLocalStorageItem = (key: string, value: string): void =>
IS_CLIENT && localStorage.setItem(key, value);

export const removeLocalStorageItem = (key: string): void =>
IS_CLIENT && localStorage.removeItem(key);

/* eslint-enable scanjs-rules/identifier_localStorage */

0 comments on commit f013b0b

Please sign in to comment.