Skip to content

Commit 86359c9

Browse files
authored
fix: do not fail if localStorage access is denied (#23)
If access to local storage is denied (e.g. by denying access to cookies in Safari) the setup of the UserCentrics.Provider will fail.
1 parent d774fad commit 86359c9

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/utils.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ export const getServicesBaseInfo = (): ServiceInfo[] =>
6363
* const myService = services.find((service) => service.id === 'my-service-id')
6464
*/
6565
export const getServicesFromLocalStorage = (): ServiceInfoFromLocalStorage[] => {
66-
const ucSettings = IS_BROWSER && localStorage?.getItem('uc_settings')
67-
if (ucSettings) {
68-
try {
66+
try {
67+
const ucSettings = IS_BROWSER && localStorage?.getItem('uc_settings')
68+
if (ucSettings) {
6969
const ucSettingsObj = JSON.parse(ucSettings) as SettingsFromLocalStorage
7070
/** Leave out any other untyped fields */
7171
return ucSettingsObj.services.map(({ id, status }) => ({ id, status }))
72-
} catch {
73-
/** Ignore failures */
7472
}
73+
} catch {
74+
/** Ignore failures */
7575
}
7676

7777
return []
@@ -87,8 +87,12 @@ export const getServicesFromLocalStorage = (): ServiceInfoFromLocalStorage[] =>
8787
* }
8888
*/
8989
export const hasUserInteracted = (): boolean => {
90-
const userInteraction = IS_BROWSER && localStorage?.getItem('uc_user_interaction')
91-
return userInteraction === 'true'
90+
try {
91+
const userInteraction = IS_BROWSER && localStorage?.getItem('uc_user_interaction')
92+
return userInteraction === 'true'
93+
} catch {
94+
return false
95+
}
9296
}
9397

9498
/**

0 commit comments

Comments
 (0)