Skip to content

Commit

Permalink
Check localStorage permission (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnutz authored Mar 3, 2021
1 parent 1e887bb commit d3bc084
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/plugins/consent-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from '../utils/lodash'
import dayjs from 'dayjs'
import global from 'global'
import generateUUID from '../utils/generateUUID'
import { camelCase } from '../utils/misc'
import { camelCase, isLocalStorageAccessible } from '../utils/misc'

require('es6-promise').polyfill()

Expand Down Expand Up @@ -178,6 +178,8 @@ export default {
},

getPreferences () {
if (!isLocalStorageAccessible()) return null

const persistedPreferences = JSON.parse(global.localStorage.getItem(this.consentManager.storageKey)) || null

if (persistedPreferences) {
Expand All @@ -199,7 +201,7 @@ export default {
},

_savePreferences () {
if (_.isEmpty(this.consentManager.preferences)) return
if (!isLocalStorageAccessible() || _.isEmpty(this.consentManager.preferences)) return

global.localStorage.setItem(
this.consentManager.storageKey,
Expand Down
5 changes: 5 additions & 0 deletions lib/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ exports.areEventsBlocked = function areEventsBlocked () {
*/
exports.setSignedMode = function setSignedMode () {
if (this.client.storeConsentByLocalStorage) {
if (!misc.isLocalStorageAccessible()) return this
global.localStorage.setItem(SIGNEDMODECOOKIE, 'true')
} else {
setCookie(this.client.storage, SIGNEDMODECOOKIE, 'true')
Expand All @@ -88,6 +89,8 @@ exports.setSignedMode = function setSignedMode () {
*/
exports.setAnonymousMode = function setAnonymousMode (keepIdentifier) {
if (this.client.storeConsentByLocalStorage) {
if (!misc.isLocalStorageAccessible()) return this

global.localStorage.setItem(SIGNEDMODECOOKIE, 'false')
} else {
setCookie(this.client.storage, SIGNEDMODECOOKIE, 'false')
Expand All @@ -114,6 +117,8 @@ exports.setAnonymousMode = function setAnonymousMode (keepIdentifier) {
*/
exports.inSignedMode = function inSignedMode () {
if (this.client.storeConsentByLocalStorage) {
if (!misc.isLocalStorageAccessible()) return false

return global.localStorage.getItem([SIGNEDMODECOOKIE]) !== 'false' &&
(global.localStorage.getItem([SIGNEDMODECOOKIE]) === 'true' ||
this.client.startInSignedMode)
Expand Down
14 changes: 13 additions & 1 deletion lib/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ function capitalizeFirstLetter (str) {
return str.slice(0, 2).toUpperCase() + str.slice(2)
}

function isLocalStorageAccessible () {
var test = '__td__'
try {
localStorage.setItem(test, test)
localStorage.removeItem(test)
return true
} catch (e) {
return false
}
}

function camelCase (str) {
if (!str) return

Expand All @@ -66,5 +77,6 @@ module.exports = {
disposable: disposable,
invariant: invariant,
fetchWithTimeout: fetchWithTimeout,
camelCase: camelCase
camelCase: camelCase,
isLocalStorageAccessible: isLocalStorageAccessible
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"afterEach",
"beforeEach",
"describe",
"it"
"it",
"localStorage"
]
},
"prettier": {
Expand Down

0 comments on commit d3bc084

Please sign in to comment.