Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
feat: secure cookies (#600)
Browse files Browse the repository at this point in the history
* feat: cookie package added

* feat: 'cookies' storage added

* fix: cookie "mcart"
  • Loading branch information
butikden authored Mar 17, 2022
1 parent 8ef60df commit c60f3d9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"fetch-everywhere": "^1.0.5",
"form-data": "^4.0.0",
"inflected": "^2.0.1",
"js-cookie": "^3.0.1",
"node-localstorage": "^2.1.6"
},
"config": {
Expand Down
8 changes: 7 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { version } from '../package.json'
import LocalStorageFactory from './factories/local-storage'
import SecureCookiesStorageFactory from './factories/secure-cookies-storage'

class Config {
constructor(options) {
Expand All @@ -11,6 +12,7 @@ class Config {
language,
host,
storage,
storage_type,
custom_fetch,
custom_authenticator,
headers,
Expand Down Expand Up @@ -40,7 +42,11 @@ class Config {
version,
language: 'JS'
}
this.storage = storage || new LocalStorageFactory()
const defaultStorage =
storage_type === 'cookies'
? new SecureCookiesStorageFactory()
: new LocalStorageFactory()
this.storage = storage || defaultStorage
this.custom_authenticator = custom_authenticator
this.headers = headers || {}
this.disableCart = disableCart || false
Expand Down
21 changes: 21 additions & 0 deletions src/factories/secure-cookies-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Cookies from 'js-cookie'

class SecureCookiesStorageFactory {
constructor() {
this.cookies = Cookies
}

set(key, value) {
return this.cookies.set(key, value, { secure: true, sameSite: 'strict' })
}

get(key) {
return this.cookies.get(key)
}

delete(key) {
return this.cookies.remove(key)
}
}

export default SecureCookiesStorageFactory
2 changes: 2 additions & 0 deletions src/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ConfigOptions {
custom_fetch?: Function
custom_authenticator?: () => Promise<CustomAuthenticatorResponseBody>
storage?: StorageFactory
storage_type?: 'cookies' | 'localStorage'
headers?: { [key: string]: string }
disableCart?: Boolean
reauth?: Boolean,
Expand Down Expand Up @@ -58,6 +59,7 @@ export interface Config {
language: 'JS'
}
storage?: StorageFactory
storage_type?: 'cookies' | 'localStorage'
retryDelay?: number
retryJitter?: number

Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createCartIdentifier() {
export function cartIdentifier(storage) {
const cartId = createCartIdentifier()

if (storage.get('mcart') !== null) {
if (storage.get('mcart') !== null && storage.get('mcart') !== undefined) {
return storage.get('mcart')
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3378,6 +3378,11 @@ jest-validate@^23.5.0:
leven "^2.1.0"
pretty-format "^23.6.0"

js-cookie@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-3.0.1.tgz#9e39b4c6c2f56563708d7d31f6f5f21873a92414"
integrity sha512-+0rgsUXZu4ncpPxRL+lNEptWMOWl9etvPHc/koSRp6MPwpRYAhmk0dUG00J4bxVV3r9uUzfo24wW0knS07SKSw==

js-levenshtein@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
Expand Down

0 comments on commit c60f3d9

Please sign in to comment.