-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ DOP-4358 pull out some functions into its own utils to be reused, …
…along with creating a currentUserCleanup function (#1009) Co-authored-by: Caesar Bell <[email protected]>
- Loading branch information
1 parent
f4ef12b
commit c4f12a4
Showing
3 changed files
with
45 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters