adobe / alloy

Alloy is the web SDK for the Adobe Experience Platform.
Apache License 2.0
94 stars 45 forks source link

Fixed an issue where cookies destinations were not set to the correct path. #1160

Closed dompuiu closed 1 month ago

dompuiu commented 1 month ago

Description

  1. Let's say you start with a js-cookie instance:
import export default {
  get: cookie.get,
  set: cookie.set,
  remove: cookie.remove,
  withConverter: cookie.withConverter
}; from 'js-cookie'

You will get back an object that has the regular methods (get, set, remove). The object contains also an attributes property that by default has the value {path: '/'}. If you then call

Cookies.set('name', 'value');

the cookie's path will be /.

  1. If then we would do:
Cookies = Cookies.withConverter({ write: function () { .... } });
Cookies.set('name', 'value');

The cookie's path will still be / in this case. js-cookie is supporting this scenario here. Pay attention that the code uses this.attributes.

  1. We are using js-cookies in a different way here:
export default {
  get: cookie.get,
  set: cookie.set,
  remove: cookie.remove,
  withConverter: cookie.withConverter
};

We then call in the Audience module cookieJar.withConverter. At this point, we will get back as instance without the default path being /. That happens because the this.attributes in js-cookie code will refer to the object that we export in the cookieJar.js file. And that object doesn't contain any attributes property.

  1. Initially I was thinking to just export the js-cookie module inside cookieJar.js. The cookieJar code would have become something like this:
import cookie from "js-cookie";
export default cookie;

There is a problem though. The Cookies variable that is imported is an object that contains 2 properties: attributes and converter.

The methods get, set and remove are set on the prototype chain of that object.

And when we do here

 return {
    ...cookieJar,
    set(key, value, options) {
        ...
    },
  };

the spread operation is working only on the properties. Not on the whole prototype chain. The result was that you would have got back an object that didn't contain get or remove properties anymore. This is why I got to the solution that Jon suggested to bind the js-cookie initial context.

Related Issue

https://jira.corp.adobe.com/browse/PLATIR-42315

Motivation and Context

Screenshots (if appropriate):

Types of changes

Checklist: