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

fix: race condition in livechat popout mode cross origin #34158

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-mails-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/livechat": patch
---

Fixes issue that caused different sessions when opening a livechat popover in cross domain
18 changes: 15 additions & 3 deletions packages/livechat/src/components/Screen/ScreenProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback, useContext, useEffect, useState } from 'preact/hooks';
import { parse } from 'query-string';

import { isActiveSession } from '../../helpers/isActiveSession';
import { createOrUpdateGuest, evaluateChangesAndLoadConfigByFields } from '../../lib/hooks';
import { loadConfig } from '../../lib/main';
import { parentCall } from '../../lib/parentCall';
import { loadMessages } from '../../lib/room';
Expand Down Expand Up @@ -76,7 +77,7 @@ export const ScreenProvider: FunctionalComponent = ({ children }) => {
} = useContext(StoreContext);
const { department, name, email } = iframe.guest || {};
const { color, position: configPosition, background } = config.theme || {};
const { livechatLogo, hideWatermark = false } = config.settings || {};
const { livechatLogo, hideWatermark = false, registrationForm } = config.settings || {};

const {
color: customColor,
Expand Down Expand Up @@ -137,15 +138,26 @@ export const ScreenProvider: FunctionalComponent = ({ children }) => {

const dismissNotification = () => !isActiveSession();

const checkPoppedOutWindow = useCallback(() => {
const checkPoppedOutWindow = useCallback(async () => {
// Checking if the window is poppedOut and setting parent minimized if yes for the restore purpose
const poppedOut = parse(window.location.search).mode === 'popout';
const { token = '' } = parse(window.location.search);
setPopedOut(poppedOut);

if (poppedOut) {
dispatch({ minimized: false, undocked: true });
}
}, [dispatch]);

if (token && typeof token === 'string') {
if (registrationForm && !name && !email) {
dispatch({ token });
} else {
await evaluateChangesAndLoadConfigByFields(async () => {
await createOrUpdateGuest({ token });
});
}
MartinSchoeler marked this conversation as resolved.
Show resolved Hide resolved
}
}, [dispatch, email, name, registrationForm]);

useEffect(() => {
checkPoppedOutWindow();
Expand Down
4 changes: 2 additions & 2 deletions packages/livechat/src/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createToken } from './random';
import { loadMessages } from './room';
import Triggers from './triggers';

const evaluateChangesAndLoadConfigByFields = async (fn: () => Promise<void>) => {
export const evaluateChangesAndLoadConfigByFields = async (fn: () => Promise<void>) => {
const oldStore = JSON.parse(
JSON.stringify({
user: store.state.user || {},
Expand Down Expand Up @@ -42,7 +42,7 @@ const evaluateChangesAndLoadConfigByFields = async (fn: () => Promise<void>) =>
}
};

const createOrUpdateGuest = async (guest: StoreState['guest']) => {
export const createOrUpdateGuest = async (guest: StoreState['guest']) => {
if (!guest) {
return;
}
Expand Down
13 changes: 3 additions & 10 deletions packages/livechat/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,13 @@ const api: InternalWidgetAPI = {
if (!config.url) {
throw new Error('Config.url is not set!');
}
const urlToken = token && `&token=${token}`;

api.popup = window.open(
`${config.url}${config.url.lastIndexOf('?') > -1 ? '&' : '?'}mode=popout`,
`${config.url}${config.url.lastIndexOf('?') > -1 ? '&' : '?'}mode=popout${urlToken}`,
MarcosSpessatto marked this conversation as resolved.
Show resolved Hide resolved
'livechat-popout',
`width=${WIDGET_OPEN_WIDTH}, height=${widgetHeight}, toolbars=no`,
);

const data = {
src: 'rocketchat',
fn: 'setGuestToken',
args: [token],
};

api.popup?.postMessage(data, '*');
api.popup?.focus();
},

removeWidget() {
Expand Down
Loading