Skip to content

Commit

Permalink
fix: add sso auto login config
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk committed Dec 19, 2024
1 parent bd79e77 commit e49d221
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/global/common/system/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type FastGPTFeConfigsType = {
icon?: string;
title?: string;
url?: string;
autoLogin?: boolean;
};
oauth?: {
github?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OAuthEnum } from '@fastgpt/global/support/user/constant';
import MyIcon from '@fastgpt/web/components/common/Icon';
import { customAlphabet } from 'nanoid';
import { useRouter } from 'next/router';
import { Dispatch, useMemo, useRef } from 'react';
import { Dispatch, useEffect, useMemo, useRef } from 'react';
import { useTranslation } from 'next-i18next';
import I18nLngSelector from '@/components/Select/I18nLngSelector';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
Expand Down Expand Up @@ -100,6 +100,26 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
[feConfigs?.sso?.url, oAuthList.length]
);

const formatUrl = useMemo(() => {
if (feConfigs?.sso?.url) {
return `${feConfigs.sso.url}/login/oauth/authorize?redirect_uri=${encodeURIComponent(redirectUri)}&state=${state.current}`;
}
return '';
}, [feConfigs.sso, redirectUri]);

useEffect(() => {
if (formatUrl && feConfigs?.sso?.autoLogin) {
setLoginStore({
provider: OAuthEnum.sso,
lastRoute,
state: state.current
});

window.open(formatUrl, '_self');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [feConfigs.sso]);

return (
<Flex flexDirection={'column'} h={'100%'}>
<Flex alignItems={'center'} justify={'space-between'}>
Expand Down Expand Up @@ -167,8 +187,7 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
borderRadius={'sm'}
leftIcon={<MyImage alt="" src={feConfigs.sso.icon as any} w="20px" />}
onClick={() => {
const url = feConfigs.sso?.url;
if (!url) {
if (!formatUrl) {
toast({
title: 'SSO URL is not set',
status: 'error'
Expand All @@ -181,7 +200,6 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
state: state.current
});

const formatUrl = `${url}/login/oauth/authorize?redirect_uri=${encodeURIComponent(redirectUri)}&state=${state.current}`;
window.open(formatUrl, '_self');
}}
>
Expand Down
2 changes: 1 addition & 1 deletion projects/app/src/web/context/useInitApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const useInitApp = () => {
if (sourceDomain) return sourceDomain;
return document.referrer;
})();
console.log(formatSourceDomain, '-=-=');

if (formatSourceDomain && !sessionStorage.getItem('sourceDomain')) {
sessionStorage.setItem('sourceDomain', formatSourceDomain);
}
Expand Down

0 comments on commit e49d221

Please sign in to comment.