Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
[IMPROVE] Add cookie to identify widget calls (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Sep 14, 2021
1 parent 0ae132a commit 02b4d33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import queryString from 'query-string';
const host = window.SERVER_URL
|| queryString.parse(window.location.search).serverUrl
|| (process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : null);
const useSsl = host && host.match(/^https:/) !== null;
export const useSsl = host && host.match(/^https:/) !== null;

export const Livechat = new LivechatClient({ host, protocol: 'ddp', useSsl });
4 changes: 3 additions & 1 deletion src/components/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Register from '../../routes/Register';
import SwitchDepartment from '../../routes/SwitchDepartment';
import TriggerMessage from '../../routes/TriggerMessage';
import { Provider as StoreProvider, Consumer as StoreConsumer, store } from '../../store';
import { visibility, isActiveSession } from '../helpers';
import { visibility, isActiveSession, setInitCookies } from '../helpers';

function isRTL(s) {
const rtlChars = '\u0591-\u07FF\u200F\u202B\u202E\uFB1D-\uFDFD\uFE70-\uFEFC';
Expand Down Expand Up @@ -54,6 +54,8 @@ export class App extends Component {
user,
} = this.props;

setInitCookies();

if (gdprRequired && !gdprAccepted) {
return route('/gdpr');
}
Expand Down
18 changes: 14 additions & 4 deletions src/components/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from 'preact';

import { Livechat } from '../api';
import { Livechat, useSsl } from '../api';
import I18n from '../i18n';
import store from '../store';

Expand Down Expand Up @@ -103,10 +103,20 @@ export function upsert(array = [], item, predicate, ranking) {
return array;
}

// This will allow widgets that are on different domains to send cookies to the server
// The default config for same-site (lax) dissalows to send a cookie to a "3rd party" unless the user performs an action
// like a click. Secure flag is required when SameSite is set to None
const getSecureCookieSettings = () => (useSsl ? 'SameSite=None; Secure;' : '');

export const setInitCookies = () => {
document.cookie = `rc_is_widget=t; path=/; ${ getSecureCookieSettings() }`;
document.cookie = `rc_room_type=l; path=/; ${ getSecureCookieSettings() }`;
};

export const setCookies = (rid, token) => {
document.cookie = `rc_rid=${ rid }; path=/`;
document.cookie = `rc_token=${ token }; path=/`;
document.cookie = 'rc_room_type=l; path=/';
document.cookie = `rc_rid=${ rid }; path=/; ${ getSecureCookieSettings() }`;
document.cookie = `rc_token=${ token }; path=/; ${ getSecureCookieSettings() }`;
document.cookie = `rc_room_type=l; path=/; ${ getSecureCookieSettings() }`;
};

export const createToken = () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
Expand Down

0 comments on commit 02b4d33

Please sign in to comment.