Skip to content

Commit

Permalink
fix(notifications): generating and moving clientId to preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
emacoricciati committed Oct 4, 2024
1 parent 3ac5546 commit 5e52fbb
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 11 deletions.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ PODS:
- React-Core
- react-native-geolocation (3.1.0):
- React-Core
- react-native-get-random-values (1.11.0):
- React-Core
- react-native-html-to-pdf (0.12.0):
- React-Core
- react-native-menu (0.9.1):
Expand Down Expand Up @@ -793,6 +795,7 @@ DEPENDENCIES:
- react-native-date-picker (from `../node_modules/react-native-date-picker`)
- react-native-document-picker (from `../node_modules/react-native-document-picker`)
- "react-native-geolocation (from `../node_modules/@react-native-community/geolocation`)"
- react-native-get-random-values (from `../node_modules/react-native-get-random-values`)
- react-native-html-to-pdf (from `../node_modules/react-native-html-to-pdf`)
- "react-native-menu (from `../node_modules/@react-native-menu/menu`)"
- "react-native-netinfo (from `../node_modules/@react-native-community/netinfo`)"
Expand Down Expand Up @@ -930,6 +933,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native-document-picker"
react-native-geolocation:
:path: "../node_modules/@react-native-community/geolocation"
react-native-get-random-values:
:path: "../node_modules/react-native-get-random-values"
react-native-html-to-pdf:
:path: "../node_modules/react-native-html-to-pdf"
react-native-menu:
Expand Down Expand Up @@ -1075,6 +1080,7 @@ SPEC CHECKSUMS:
react-native-date-picker: 1d61c76315098a6ad4c643a356f005916c7d4875
react-native-document-picker: cd4d6b36a5207ad7a9e599ebb9eb0c2e84fa0b87
react-native-geolocation: ef66fb798d96284c6043f0b16c15d9d1d4955db4
react-native-get-random-values: 21325b2244dfa6b58878f51f9aa42821e7ba3d06
react-native-html-to-pdf: 4c5c6e26819fe202971061594058877aa9b25265
react-native-menu: 9728f90160c36b9a75481fc76e05354b99d54c59
react-native-netinfo: 48c5f79a84fbc3ba1d28a8b0d04adeda72885fa8
Expand Down
46 changes: 41 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@miblanchard/react-native-slider": "^2.2.0",
"@openspacelabs/react-native-zoomable-view": "^2.1.5",
"@orama/orama": "^2.0.0-beta.8",
"@polito/api-client": "^1.0.0-ALPHA.64",
"@polito/api-client": "^1.0.0-ALPHA.65",
"@react-native-async-storage/async-storage": "^1.18.2",
"@react-native-clipboard/clipboard": "^1.12.1",
"@react-native-community/blur": "^4.3.0",
Expand All @@ -49,6 +49,7 @@
"@tanstack/react-query": "^4.29.18",
"@tanstack/react-query-persist-client": "^4.33.0",
"@types/luxon": "^3.2.0",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.50.0",
"color": "^4.2.3",
"domutils": "^3.0.1",
Expand All @@ -73,6 +74,7 @@
"react-native-file-viewer": "^2.1.5",
"react-native-fs": "^2.20.0",
"react-native-gesture-handler": "^2.13.1",
"react-native-get-random-values": "^1.11.0",
"react-native-html-to-pdf": "^0.12.0",
"react-native-image-crop-picker": "^0.38.0",
"react-native-keyboard-accessory": "^0.1.16",
Expand All @@ -96,7 +98,8 @@
"react-string-replace": "^1.1.0",
"react-usestateref": "^1.0.8",
"semver": "^7.5.4",
"superjson": "^1.13.1"
"superjson": "^1.13.1",
"uuid": "^10.0.0"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
2 changes: 2 additions & 0 deletions src/core/contexts/PreferencesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AgendaTypesFilterState } from '../../features/agenda/types/AgendaTypesF

export const editablePreferenceKeys = [
'lastInstalledVersion',
'clientId',
'username',
'campusId',
'colorScheme',
Expand Down Expand Up @@ -43,6 +44,7 @@ export type CoursesPreferences = {

export interface PreferencesContextBase {
lastInstalledVersion: string | null;
clientId?: string;
username: string;
campusId?: string;
colorScheme: 'light' | 'dark' | 'system';
Expand Down
27 changes: 27 additions & 0 deletions src/core/migrations/1.6.7/migrateClientIdToPreferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'react-native-get-random-values';
import * as Keychain from 'react-native-keychain';

import { v4 as uuidv4 } from 'uuid';

import { PreferencesContextProps } from '../../contexts/PreferencesContext';

export const migrateClientIdToPreferences = async (
preferences: PreferencesContextProps,
) => {
const { updatePreference } = preferences;
try {
const credentials = await Keychain.getGenericPassword();
let clientId;
if (credentials && credentials.username) {
clientId = credentials.username;
} else {
clientId = uuidv4();
}
updatePreference('clientId', clientId);
} catch (e) {
console.warn(
"Keychain couldn't be accessed! Failed to migrate clientId.",
e,
);
}
};
5 changes: 5 additions & 0 deletions src/core/migrations/MigrationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PreferencesContextProps } from '../contexts/PreferencesContext';
import { Migration } from '../types/migrations';
import { storeCoursePreferencesByShortcode } from './1.5.0/storeCoursePreferencesByShortcode';
import { migrateCourseFilesCacheToDocumentsDirectory } from './1.6.2/migrateCourseFilesCacheToDocumentsDirectory';
import { migrateClientIdToPreferences } from './1.6.7/migrateClientIdToPreferences';
import { invalidateCache } from './common';

export class MigrationService {
Expand All @@ -19,6 +20,10 @@ export class MigrationService {
runBeforeVersion: '1.6.2',
run: [migrateCourseFilesCacheToDocumentsDirectory],
},
{
runBeforeVersion: '1.6.7',
run: [migrateClientIdToPreferences],
},
];

static needsMigration(preferences: PreferencesContextProps) {
Expand Down
11 changes: 7 additions & 4 deletions src/core/queries/authHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ async function getFcmToken(): Promise<string | undefined> {
export const useLogin = () => {
const authClient = useAuthClient();
const { refreshContext } = useApiContext();
const { updatePreference } = usePreferencesContext();
const { updatePreference, clientId } = usePreferencesContext();

return useMutation({
mutationFn: (dto: LoginRequest) => {
const client = { name: 'Students app', id: 'students-app' };
const client = {
name: 'Students app',
...(clientId && { id: clientId }),
};

return Promise.all([
DeviceInfo.getDeviceName(),
Expand Down Expand Up @@ -84,10 +87,10 @@ export const useLogin = () => {
});
},
onSuccess: async data => {
const { token, clientId, username } = data;
const { token, clientId: cid, username } = data;
refreshContext({ username, token });
updatePreference('username', username);
await Keychain.setGenericPassword(clientId, token);
await Keychain.setGenericPassword(cid, token);
},
});
};
Expand Down

0 comments on commit 5e52fbb

Please sign in to comment.