Skip to content

Commit b597e76

Browse files
committed
Add a method to delete cookies set on lower level domains
1 parent 387bd35 commit b597e76

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/amplitude-client.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import UUID from './uuid';
1414
import base64Id from './base64Id';
1515
import { version } from '../package.json';
1616
import DEFAULT_OPTIONS from './options';
17+
import getHost from './get-host';
18+
import baseCookie from './base-cookie';
1719

1820
let AsyncStorage;
1921
let Platform;
@@ -248,6 +250,31 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
248250
}
249251
};
250252

253+
AmplitudeClient.prototype.deleteLowerLevelDomainCookies = function () {
254+
const host = getHost();
255+
256+
const cookieHost =
257+
(this.options.domain && this.options.domain[0] === '.') ?
258+
this.options.domain.slice(1) : this.options.domain;
259+
260+
if (!cookieHost) {
261+
return;
262+
}
263+
264+
if (host !== cookieHost) {
265+
if (new RegExp(cookieHost + '$').test(host)) {
266+
const hostParts = host.split('.');
267+
const cookieHostParts = cookieHost.split('.');
268+
269+
for (let i = hostParts.length; i > cookieHostParts.length; --i) {
270+
const deleteDomain = hostParts.slice(hostParts.length - i).join('.');
271+
baseCookie.set(this._cookieName, null, {domain: '.' + deleteDomain});
272+
}
273+
baseCookie.set(this._cookieName, null, {});
274+
}
275+
}
276+
};
277+
251278
AmplitudeClient.prototype._getInitialDeviceId = function (configDeviceId, storedDeviceId) {
252279
if (configDeviceId) {
253280
return configDeviceId;

src/get-host.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const getHost = (url) => {
2+
const a = document.createElement('a');
3+
a.href = url;
4+
return a.hostname || location.hostname;
5+
};
6+
7+
export default getHost;

src/top-domain.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import baseCookie from './base-cookie';
22
import base64Id from './base64Id';
3-
4-
const getHost = (url) => {
5-
const a = document.createElement('a');
6-
a.href = url;
7-
return a.hostname || location.hostname;
8-
};
3+
import getHost from './get-host';
94

105
const topDomain = (url) => {
116
const host = getHost(url);

0 commit comments

Comments
 (0)