diff --git a/.changeset/green-ants-shop.md b/.changeset/green-ants-shop.md new file mode 100644 index 0000000000000..74a11f9c9e848 --- /dev/null +++ b/.changeset/green-ants-shop.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': minor +--- + +Fixes login using iframe authentication. diff --git a/apps/meteor/client/hooks/iframe/useIframe.ts b/apps/meteor/client/hooks/iframe/useIframe.ts index 78677d7c45421..64d2b4aa99f80 100644 --- a/apps/meteor/client/hooks/iframe/useIframe.ts +++ b/apps/meteor/client/hooks/iframe/useIframe.ts @@ -1,5 +1,5 @@ import { useLoginWithIframe, useLoginWithToken, useSetting } from '@rocket.chat/ui-contexts'; -import { useCallback, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; export const useIframe = () => { const [iframeLoginUrl, setIframeLoginUrl] = useState(undefined); @@ -12,6 +12,8 @@ export const useIframe = () => { const iframeLogin = useLoginWithIframe(); const tokenLogin = useLoginWithToken(); + const enabled = Boolean(iframeEnabled && accountIframeUrl && apiUrl && apiMethod); + const loginWithToken = useCallback( (tokenData: string | { loginToken: string } | { token: string }, callback?: (error: Error | null | undefined) => void) => { if (typeof tokenData === 'string') { @@ -31,6 +33,10 @@ export const useIframe = () => { const tryLogin = useCallback( async (callback?: (error: Error | null | undefined, result: unknown) => void) => { + if (!enabled) { + return; + } + let url = accountIframeUrl; let separator = '?'; if (url.indexOf('?') > -1) { @@ -43,9 +49,7 @@ export const useIframe = () => { const result = await fetch(apiUrl, { method: apiMethod, - headers: { - 'Content-Type': 'application/json', - }, + headers: undefined, credentials: 'include', }); @@ -64,11 +68,15 @@ export const useIframe = () => { callback?.(error, await result.json()); }); }, - [apiMethod, apiUrl, accountIframeUrl, loginWithToken], + [apiMethod, apiUrl, accountIframeUrl, loginWithToken, enabled], ); + useEffect(() => { + tryLogin(); + }, [tryLogin]); + return { - enabled: Boolean(iframeEnabled && accountIframeUrl && apiUrl && apiMethod), + enabled, tryLogin, loginWithToken, iframeLoginUrl,