Skip to content

Commit 652ff73

Browse files
regression: iframe auth api request loop (RocketChat#37021)
Co-authored-by: Tasso Evangelista <2263066+tassoevan@users.noreply.github.com>
1 parent 411f130 commit 652ff73

2 files changed

Lines changed: 47 additions & 43 deletions

File tree

apps/meteor/client/hooks/iframe/useIframe.ts

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
12
import { useLoginWithIframe, useLoginWithToken, useSetting } from '@rocket.chat/ui-contexts';
2-
import { useCallback, useEffect, useState } from 'react';
3+
import { useCallback, useState } from 'react';
34

45
export const useIframe = () => {
56
const [iframeLoginUrl, setIframeLoginUrl] = useState<string | undefined>(undefined);
@@ -31,54 +32,48 @@ export const useIframe = () => {
3132
[iframeLogin, tokenLogin],
3233
);
3334

34-
const tryLogin = useCallback(
35-
async (callback?: (error: Error | null | undefined, result: unknown) => void) => {
36-
if (!enabled) {
37-
return;
38-
}
39-
40-
let url = accountIframeUrl;
41-
let separator = '?';
42-
if (url.indexOf('?') > -1) {
43-
separator = '&';
44-
}
35+
const tryLogin = useEffectEvent(async (callback?: (error: Error | null | undefined, result: unknown) => void) => {
36+
if (!enabled) {
37+
return;
38+
}
4539

46-
if (navigator.userAgent.indexOf('Electron') > -1) {
47-
url += `${separator}client=electron`;
48-
}
40+
let url = accountIframeUrl;
41+
let separator = '?';
42+
if (url.indexOf('?') > -1) {
43+
separator = '&';
44+
}
4945

50-
try {
51-
const result = await fetch(apiUrl, {
52-
method: apiMethod,
53-
headers: undefined,
54-
credentials: 'include',
55-
});
46+
if (navigator.userAgent.indexOf('Electron') > -1) {
47+
url += `${separator}client=electron`;
48+
}
5649

57-
if (!result.ok || result.status !== 200) {
58-
setIframeLoginUrl(url);
59-
callback?.(new Error(), null);
60-
return;
61-
}
50+
try {
51+
const result = await fetch(apiUrl, {
52+
method: apiMethod,
53+
headers: undefined,
54+
credentials: 'include',
55+
});
6256

63-
loginWithToken(await result.json(), async (error: Meteor.Error | Meteor.TypedError | Error | null | undefined) => {
64-
if (error) {
65-
setIframeLoginUrl(url);
66-
} else {
67-
setIframeLoginUrl(undefined);
68-
}
69-
callback?.(error, await result.json());
70-
});
71-
} catch (error) {
57+
if (!result.ok || result.status !== 200) {
7258
setIframeLoginUrl(url);
73-
callback?.(error instanceof Error ? error : undefined, null);
59+
callback?.(new Error(), null);
60+
return;
7461
}
75-
},
76-
[apiMethod, apiUrl, accountIframeUrl, loginWithToken, enabled],
77-
);
7862

79-
useEffect(() => {
80-
tryLogin();
81-
}, [tryLogin]);
63+
const body = await result.json();
64+
loginWithToken(body, async (error: Meteor.Error | Meteor.TypedError | Error | null | undefined) => {
65+
if (error) {
66+
setIframeLoginUrl(url);
67+
} else {
68+
setIframeLoginUrl(undefined);
69+
}
70+
callback?.(error, body);
71+
});
72+
} catch (error) {
73+
setIframeLoginUrl(url);
74+
callback?.(error instanceof Error ? error : undefined, null);
75+
}
76+
});
8277

8378
return {
8479
enabled,

apps/meteor/client/views/root/MainLayout/LoginPage.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useSession } from '@rocket.chat/ui-contexts';
22
import type { LoginRoutes } from '@rocket.chat/web-ui-registration';
33
import RegistrationRoute from '@rocket.chat/web-ui-registration';
44
import type { ReactElement, ReactNode } from 'react';
5+
import { useEffect } from 'react';
56
import { useTranslation } from 'react-i18next';
67

78
import LoggedOutBanner from '../../../components/deviceManagement/LoggedOutBanner';
@@ -10,7 +11,15 @@ import { useIframe } from '../../../hooks/iframe/useIframe';
1011
const LoginPage = ({ defaultRoute, children }: { defaultRoute?: LoginRoutes; children?: ReactNode }): ReactElement => {
1112
const { t } = useTranslation();
1213
const showForcedLogoutBanner = useSession('forceLogout') as boolean | undefined;
13-
const { iframeLoginUrl } = useIframe();
14+
const { iframeLoginUrl, tryLogin, enabled: iframeEnabled } = useIframe();
15+
16+
useEffect(() => {
17+
if (!iframeEnabled) {
18+
return;
19+
}
20+
21+
tryLogin();
22+
}, [tryLogin, iframeEnabled]);
1423

1524
if (iframeLoginUrl) {
1625
return <iframe title={t('Login')} src={iframeLoginUrl} style={{ height: '100%', width: '100%' }} />;

0 commit comments

Comments
 (0)