Skip to content

Commit

Permalink
Refactor[INJI-467] : encrypt the missed data which is stored unencryp…
Browse files Browse the repository at this point in the history
…ted. (#981)

* refactor(inji-467): encrypt the missed data which is stored unencrypted.

Signed-off-by: Vijay <[email protected]>

* refactor(inji-467): remove additional i18n import statement

Signed-off-by: Vijay <[email protected]>

---------

Signed-off-by: Vijay <[email protected]>
  • Loading branch information
vijay151096 authored Nov 2, 2023
1 parent 61ec52a commit 6fcff9c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
46 changes: 24 additions & 22 deletions components/LanguageSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,51 @@ import React from 'react';
import {SUPPORTED_LANGUAGES} from '../i18n';
import {I18nManager, View} from 'react-native';
import {Picker} from './ui/Picker';
import Storage from '../shared/storage';
import {useTranslation} from 'react-i18next';
import i18next from 'i18next';
import i18next, {i18n} from 'i18next';
import RNRestart from 'react-native-restart';
import {setItem} from '../machines/store';
import Keychain from 'react-native-keychain';

export const LanguageSelector: React.FC<LanguageSelectorProps> = props => {
const {i18n} = useTranslation();
const languages = Object.entries(SUPPORTED_LANGUAGES).map(
([value, label]) => ({label, value}),
);

const changeLanguage = async (language: string) => {
if (language !== i18n.language) {
await i18n.changeLanguage(language).then(async () => {
await Storage.setItem('language', i18n.language);
const isRTL = i18next.dir(language) === 'rtl' ? true : false;
if (isRTL !== I18nManager.isRTL) {
try {
I18nManager.forceRTL(isRTL);
setTimeout(() => {
RNRestart.Restart();
}, 150);
} catch (e) {
console.log('error', e);
}
}
});
}
};

return (
<View>
<Picker
testID="language"
items={languages}
selectedValue={i18n.language}
onValueChange={changeLanguage}
onValueChange={language => changeLanguage(i18n, language)}
triggerComponent={props.triggerComponent}
/>
</View>
);
};

export const changeLanguage = async (i18n: i18n, language: string) => {
if (language !== i18n.language) {
await i18n.changeLanguage(language).then(async () => {
const existingCredentials = await Keychain.getGenericPassword();
await setItem('language', i18n.language, existingCredentials.password);
const isRTL = i18next.dir(language) === 'rtl' ? true : false;
if (isRTL !== I18nManager.isRTL) {
try {
I18nManager.forceRTL(isRTL);
setTimeout(() => {
RNRestart.Restart();
}, 150);
} catch (e) {
console.log('error', e);
}
}
});
}
};

interface LanguageSelectorProps {
triggerComponent: React.ReactElement;
}
10 changes: 8 additions & 2 deletions i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import ar from './locales/ara.json';
import hi from './locales/hin.json';
import kn from './locales/kan.json';
import ta from './locales/tam.json';
import Storage from './shared/storage';

import {iso6393To1} from 'iso-639-3';
import {LocalizedField} from './types/VC/ExistingMosipVC/vc';
import Keychain from 'react-native-keychain';
import {getItem} from './machines/store';

const resources = {en, fil, ar, hi, kn, ta};
const locale = Localization.locale;
Expand All @@ -36,7 +37,12 @@ i18next
supportedLngs: Object.keys(SUPPORTED_LANGUAGES),
})
.then(async () => {
const language = await Storage.getItem('language');
const existingCredentials = await Keychain.getGenericPassword();
const language = await getItem(
'language',
null,
existingCredentials.password,
);
if (language !== i18next.language) {
i18next.changeLanguage(language);
populateLanguageCodeMap();
Expand Down
26 changes: 2 additions & 24 deletions screens/SetupLanguageScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import React from 'react';
import i18n, {SUPPORTED_LANGUAGES} from '../i18n';
import {I18nManager, Dimensions} from 'react-native';
import Storage from '../shared/storage';
import {useTranslation} from 'react-i18next';
import i18next from 'i18next';
import RNRestart from 'react-native-restart';
import {SetupPicker} from '../components/ui/SetupPicker';
import {Button, Column, Text} from '../components/ui';
import {Theme} from '../components/ui/styleUtils';
import {Icon} from 'react-native-elements';
import {RootRouteProps} from '../routes';
import {useWelcomeScreen} from './WelcomeScreenController';
import {changeLanguage} from '../components/LanguageSelector';

export const SetupLanguageScreen: React.FC<RootRouteProps> = props => {
const {t} = useTranslation('SetupLanguage');
Expand All @@ -19,25 +16,6 @@ export const SetupLanguageScreen: React.FC<RootRouteProps> = props => {
([value, label]) => ({label, value}),
);

const changeLanguage = async (language: string) => {
if (language !== i18n.language) {
await i18n.changeLanguage(language).then(async () => {
await Storage.setItem('language', i18n.language);
const isRTL = i18next.dir(language) === 'rtl' ? true : false;
if (isRTL !== I18nManager.isRTL) {
try {
I18nManager.forceRTL(isRTL);
setTimeout(() => {
RNRestart.Restart();
}, 150);
} catch (e) {
console.log('error', e);
}
}
});
}
};

return (
<Column style={Theme.SetupLanguageScreenStyle.columnStyle}>
<Icon
Expand Down Expand Up @@ -67,7 +45,7 @@ export const SetupLanguageScreen: React.FC<RootRouteProps> = props => {
testID="languagePicker"
items={languages}
selectedValue={i18n.language}
onValueChange={changeLanguage}
onValueChange={language => changeLanguage(i18n, language)}
/>

<Button
Expand Down
35 changes: 25 additions & 10 deletions shared/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {request} from './request';
import Storage, {API_CACHED_STORAGE_KEYS} from './storage';
import {API_CACHED_STORAGE_KEYS} from './storage';
import {COMMON_PROPS_KEY} from './commonprops/commonProps';
import {INITIAL_CONFIG} from './InitialConfig';
import Keychain from 'react-native-keychain';
import {getItem, setItem} from '../machines/store';

export const API_URLS: ApiUrls = {
issuersList: {
Expand Down Expand Up @@ -159,17 +161,23 @@ async function generateCacheAPIFunctionWithCachePreference(
fetchCall: (...props: any[]) => any,
onErrorHardCodedValue?: any,
) {
const existingCredentials = await Keychain.getGenericPassword();
try {
const response = (await Storage.getItem(cacheKey)) as string;
const response = (await getItem(
cacheKey,
null,
existingCredentials?.password,
)) as string;

if (response) {
return JSON.parse(response);
} else {
const response = await fetchCall();

Storage.setItem(cacheKey, JSON.stringify(response)).then(() =>
console.log('Cached response for ' + cacheKey),
);
setItem(
cacheKey,
JSON.stringify(response),
existingCredentials?.password,
).then(() => console.log('Cached response for ' + cacheKey));

return response;
}
Expand All @@ -193,11 +201,14 @@ async function generateCacheAPIFunctionWithAPIPreference(
fetchCall: (...props: any[]) => any,
onErrorHardCodedValue?: any,
) {
const existingCredentials = await Keychain.getGenericPassword();
try {
const response = await fetchCall();
Storage.setItem(cacheKey, JSON.stringify(response)).then(() =>
console.log('Cached response for ' + cacheKey),
);
setItem(
cacheKey,
JSON.stringify(response),
existingCredentials.password,
).then(() => console.log('Cached response for ' + cacheKey));
return response;
} catch (error) {
console.warn(`Failed to load due to network issue in API preferred api call.
Expand All @@ -207,7 +218,11 @@ async function generateCacheAPIFunctionWithAPIPreference(

console.log(error);

const response = (await Storage.getItem(cacheKey)) as string;
const response = (await getItem(
cacheKey,
null,
existingCredentials.password,
)) as string;

if (response) {
return JSON.parse(response);
Expand Down

0 comments on commit 6fcff9c

Please sign in to comment.