fix: Iframe authentication (#36962)
Some checks failed
Code scanning - action / CodeQL-Build (push) Has been cancelled

Co-authored-by: Yash Rajpal <58601732+yash-rajpal@users.noreply.github.com>
This commit is contained in:
dionisio-bot[bot] 2025-09-17 14:25:37 +00:00 committed by GitHub
parent cee703860e
commit d7da2c0da3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': minor
---
Fixes login using iframe authentication.

View File

@ -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<string | undefined>(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,