mirror of
https://github.com/RocketChat/Rocket.Chat.git
synced 2025-12-28 06:47:25 +00:00
fix: Iframe authentication (#36962)
Some checks failed
Code scanning - action / CodeQL-Build (push) Has been cancelled
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:
parent
cee703860e
commit
d7da2c0da3
5
.changeset/green-ants-shop.md
Normal file
5
.changeset/green-ants-shop.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'@rocket.chat/meteor': minor
|
||||
---
|
||||
|
||||
Fixes login using iframe authentication.
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user