Skip to content
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

feat: ability to use custom storage for cookies #434

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gaspachoandalus
Copy link

@gaspachoandalus gaspachoandalus commented Oct 7, 2021

Motivation
Cookies are stored in the webview localStorage.
According to #194, this may be an issue for some users who might want to store cookies in native storage, possibly secured.

This PR aims to add a new interface to cordova.plugin.http to be able provide a custom (and synchronous) storage implementation:

/**
 * Provides a custom cookie storage, to override the default localStorage
 * @param {Object} storageImpl 
 * @param {(name: string, value: string) => void} storageImpl.setItem 
 * @param {(name: string) => string} storageImpl.getItem 
 * @param {(name: string) => void} storageImpl.removeItem 
 */
function setCookieStorageImpl(storageImpl)

Some trivial usage could be:

cordova.plugin.http.setCookieStorageImpl({
  setItem: (name, value) =>
  {
    console.log(`setItem cookie ${name}`);
    localStorage.setItem(`myprefix_${name}`, value);
  },
  getItem: (name) =>
  {
    console.log(`getItem cookie ${name}`);
    return localStorage.getItem(`myprefix_${name}`);
  },
  removeItem: (name) =>
  {
    console.log(`removeItem cookie ${name}`);
    localStorage.removeItem(`myprefix_${name}`);
  }
})

@gaspachoandalus
Copy link
Author

So, do you have any thoughts about this @silkimen ?

I can think of some features you might expect additionnally like:

  • asynchronous behavior, so a Promise should be returned (but some synchronous API like setCookie or getCookieString would need to be asynchronous too ; also it may require implementing internally an asynchronous ToughCookie store)
  • moving the cookie from the previous store to the new one when setCookieStorageImplis called ?

@silkimen
Copy link
Owner

Hi @gaspachoandalus, sorry for responding very late on this. 😓 Well, I like the idea of having a configurable storage, but Lindsor and also you got it correctly. All persistent storages I can think of (other than the LocalStorage) have an asynchronous API. So this feature won't help that much. I think we'll first need to check on how to allow asynchronous storages and how this affects the internals of this plugin.

@silkimen silkimen force-pushed the master branch 8 times, most recently from 1a1c9e0 to ed08534 Compare March 30, 2022 14:19
@silkimen silkimen force-pushed the master branch 7 times, most recently from 449aa28 to 6fc8a7e Compare December 29, 2022 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants