Skip to content

PM-225 Fix privacy violation related to cookie #1590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/ChallengeEditor/ChallengeView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import ChallengeTotalField from '../ChallengeTotal-Field'
import Loader from '../../Loader'
import AssignedMemberField from '../AssignedMember-Field'
import { getResourceRoleByName } from '../../../util/tc'
import { isBetaMode } from '../../../util/cookie'
import { loadGroupDetails } from '../../../actions/challenges'
import {
REVIEW_TYPES,
Expand All @@ -29,6 +28,7 @@ import {
} from '../../../config/constants'
import PhaseInput from '../../PhaseInput'
import CheckpointPrizesField from '../CheckpointPrizes-Field'
import { isBetaMode } from '../../../util/localstorage'

const ChallengeView = ({
projectDetail,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChallengeEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ import Tooltip from '../Tooltip'
import CancelDropDown from './Cancel-Dropdown'
import UseSchedulingAPIField from './UseSchedulingAPIField'

import { isBetaMode } from '../../util/cookie'
import MilestoneField from './Milestone-Field'
import DiscussionField from './Discussion-Field'
import CheckpointPrizesField from './CheckpointPrizes-Field'
import { canChangeDuration } from '../../util/phase'
import { isBetaMode } from '../../util/localstorage'

const theme = {
container: styles.modalContainer
Expand Down
6 changes: 3 additions & 3 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { saveToken } from './actions/auth'
import { loadChallengeDetails } from './actions/challenges'
import { connect } from 'react-redux'
import { checkAllowedRoles, checkOnlyReadOnlyRoles, checkReadOnlyRoles } from './util/tc'
import { setCookie, removeCookie, isBetaMode } from './util/cookie'
import IdleTimer from 'react-idle-timer'
import modalStyles from './styles/modal.module.scss'
import ConfirmationModal from './components/Modal/ConfirmationModal'
import Users from './containers/Users'
import { isBetaMode, removeFromLocalStorage, saveToLocalStorage } from './util/localstorage'

const { ACCOUNTS_APP_LOGIN_URL, IDLE_TIMEOUT_MINUTES, IDLE_TIMEOUT_GRACE_MINUTES, COMMUNITY_APP_URL } = process.env

Expand Down Expand Up @@ -105,9 +105,9 @@ class Routes extends React.Component {
const params = new URLSearchParams(search)
if (!_.isEmpty(params.get('beta'))) {
if (params.get('beta') === 'true' && !isBetaMode()) {
setCookie(BETA_MODE_COOKIE_TAG, 'true')
saveToLocalStorage(BETA_MODE_COOKIE_TAG, 'true')
} else if (params.get('beta') === 'false' && isBetaMode()) {
removeCookie(BETA_MODE_COOKIE_TAG)
removeFromLocalStorage(BETA_MODE_COOKIE_TAG)
}
this.props.history.push(this.props.location.pathname)
}
Expand Down
34 changes: 0 additions & 34 deletions src/util/cookie.js

This file was deleted.

61 changes: 61 additions & 0 deletions src/util/localstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { BETA_MODE_COOKIE_TAG } from '../config/constants'

/**
* Save an item to localStorage.
* @param {string} key - The key under which the data will be stored.
* @param {any} value - The data to store (will be stringified).
*/
export function saveToLocalStorage (key, value) {
if (!key || typeof key !== 'string') {
throw new Error('Key must be a valid string.')
}

try {
const jsonValue = JSON.stringify(value)
window.localStorage.setItem(key, jsonValue)
} catch (error) {
console.error('Failed to save to localStorage:', error)
}
}

/**
* Get an item from localStorage.
* @param {string} key - The key under which the data is stored.
* @returns {any} - The parsed data from localStorage, or null if not found.
*/
export function getFromLocalStorage (key) {
if (!key || typeof key !== 'string') {
throw new Error('Key must be a valid string.')
}

try {
const jsonValue = window.localStorage.getItem(key)
return jsonValue ? JSON.parse(jsonValue) : null
} catch (error) {
console.error('Failed to retrieve from localStorage:', error)
return null
}
}

/**
* Remove an item from localStorage.
* @param {string} key - The key of the item to remove.
*/
export function removeFromLocalStorage (key) {
if (!key || typeof key !== 'string') {
throw new Error('Key must be a valid string.')
}

try {
window.localStorage.removeItem(key)
} catch (error) {
console.error('Failed to remove from localStorage:', error)
}
}

/**
* A function that checks whether beta mode is enabled or not
*/
export function isBetaMode () {
return getFromLocalStorage(BETA_MODE_COOKIE_TAG) === 'true'
}
Loading