Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sso auto login config #3436

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading