Skip to content

Commit

Permalink
♻️ DOP-4358 pull out some functions into its own utils to be reused, …
Browse files Browse the repository at this point in the history
…along with creating a currentUserCleanup function (#1009)

Co-authored-by: Caesar Bell <[email protected]>
  • Loading branch information
caesarbell and Caesar Bell authored Feb 13, 2024
1 parent f4ef12b commit c4f12a4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/components/Widgets/FeedbackWidget/realm.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
import React from 'react';
import * as Realm from 'realm-web';
import { isBrowser } from '../../../utils/is-browser';
import { removeAllRealmUsersFromLocalStorage } from '../../../utils/realm-user-management';

const APP_ID = 'feedbackwidgetv3-dgcsv';
export const app = isBrowser ? Realm.App.getApp(APP_ID) : { auth: {} };

/**
* @param {object} storage
* @returns The prefix for the storage key used by Realm for localStorage access
*/
function parseStorageKey(storage) {
if (!storage.keyPart) {
return '';
}
const prefix = parseStorageKey(storage.storage);
return prefix + storage.keyPart + ':';
}

/**
* Deletes localStorage data for all users
*/
function deleteLocalStorageData() {
const { allUsers } = app;
// The accessToken and refreshToken are automatically removed if invalid, but not the following keys
const keysToDelete = ['profile', 'providerType'];

Object.values(allUsers).forEach((user) => {
const storageKeyPrefix = parseStorageKey(user.storage);
keysToDelete.forEach((key) => {
localStorage.removeItem(storageKeyPrefix + key);
});
});
removeAllRealmUsersFromLocalStorage(allUsers);
}

// User Authentication & Management
Expand Down
37 changes: 37 additions & 0 deletions src/utils/realm-user-management.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isBrowser } from './is-browser';
/**
* @param {object} storage
* @returns The prefix for the storage key used by Realm for localStorage access
*/
const parseStorageKey = (storage) => {
if (!storage.keyPart) {
return '';
}
const prefix = parseStorageKey(storage.storage);
return prefix + storage.keyPart + ':';
};

export const removeAllRealmUsersFromLocalStorage = (allUsers) => {
// The accessToken and refreshToken are automatically removed if invalid, but not the following keys
const keysToDelete = ['profile', 'providerType'];

Object.values(allUsers).forEach((user) => {
const storageKeyPrefix = parseStorageKey(user.storage);
keysToDelete.forEach((key) => {
localStorage.removeItem(storageKeyPrefix + key);
});
});
};

export const currentRealmUsersCleanup = (app, maxUsersAllowed = 1) => {
const { allUsers } = app;

const allUsersSize = Object.keys(allUsers).length;
if (allUsersSize > maxUsersAllowed) {
// Delete local storage for the app, removing all logged in users creds
// local store will restore new values on page navigation or refresh
if (isBrowser) {
removeAllRealmUsersFromLocalStorage(allUsers);
}
}
};
6 changes: 6 additions & 0 deletions src/utils/realm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Realm from 'realm-web';
import { SNOOTY_REALM_APP_ID } from '../build-constants';
import { currentRealmUsersCleanup } from './realm-user-management';

const app = new Realm.App({ id: SNOOTY_REALM_APP_ID });

Expand All @@ -13,6 +14,11 @@ const loginAnonymous = async () => {
return loginDefer;
}

// Clears realm users from localStorage if there are more than
// n number of users.
// n = second param
currentRealmUsersCleanup(app, 5);

// Avoid creating multiple users if one already exists
if (app.currentUser) {
return;
Expand Down

0 comments on commit c4f12a4

Please sign in to comment.