diff --git a/packages/global/common/system/types/index.d.ts b/packages/global/common/system/types/index.d.ts index 48ce70ee5..6ac0a0a95 100644 --- a/packages/global/common/system/types/index.d.ts +++ b/packages/global/common/system/types/index.d.ts @@ -45,6 +45,11 @@ export type FastGPTFeConfigsType = { isPlus?: boolean; show_phoneLogin?: boolean; show_emailLogin?: boolean; + sso?: { + icon?: string; + title?: string; + url?: string; + }; oauth?: { github?: string; google?: string; diff --git a/projects/app/src/components/Layout/auth.tsx b/projects/app/src/components/Layout/auth.tsx index 20053cfcb..52d6b5225 100644 --- a/projects/app/src/components/Layout/auth.tsx +++ b/projects/app/src/components/Layout/auth.tsx @@ -9,6 +9,7 @@ const unAuthPage: { [key: string]: boolean } = { '/login': true, '/login/provider': true, '/login/fastlogin': true, + '/login/sso': true, '/appStore': true, '/chat/share': true, '/chat/team': true, diff --git a/projects/app/src/pages/login/components/LoginForm/components/FormLayout.tsx b/projects/app/src/pages/login/components/LoginForm/components/FormLayout.tsx index 1a0ce735f..2bc00451f 100644 --- a/projects/app/src/pages/login/components/LoginForm/components/FormLayout.tsx +++ b/projects/app/src/pages/login/components/LoginForm/components/FormLayout.tsx @@ -126,6 +126,22 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => { ))} + + {feConfigs?.sso && ( + + + + )} )} diff --git a/projects/app/src/pages/login/sso.tsx b/projects/app/src/pages/login/sso.tsx new file mode 100644 index 000000000..50b5eef77 --- /dev/null +++ b/projects/app/src/pages/login/sso.tsx @@ -0,0 +1,47 @@ +import React, { useCallback, useEffect, useMemo } from 'react'; +import { useRouter } from 'next/router'; +import type { ResLogin } from '@/global/support/api/userRes.d'; +import { useChatStore } from '@/web/core/chat/context/storeChat'; +import { useUserStore } from '@/web/support/user/useUserStore'; +import { clearToken, setToken } from '@/web/support/user/auth'; +import { ssoLogin } from '@/web/support/user/api'; +import Loading from '@fastgpt/web/components/common/MyLoading'; +import { useTranslation } from 'next-i18next'; +import { useRequest2 } from '@fastgpt/web/hooks/useRequest'; + +const provider = () => { + const { t } = useTranslation(); + const { setLastChatId, setLastChatAppId } = useChatStore(); + const { setUserInfo } = useUserStore(); + const router = useRouter(); + const { query } = router; + + const loginSuccess = useCallback( + (res: ResLogin) => { + console.log('loginSuccess', res); + setToken(res.token); + setUserInfo(res.user); + // init store + setLastChatId(''); + setLastChatAppId(''); + router.push('/app/list'); + }, + [setLastChatId, setLastChatAppId, setUserInfo, router] + ); + + const { run: handleSSO } = useRequest2(() => ssoLogin(query), { + onSuccess: loginSuccess, + errorToast: t('common:support.user.login.error') + }); + + useEffect(() => { + if (query && Object.keys(query).length > 0) { + clearToken(); + handleSSO(); + } + }, [handleSSO, query]); + + return ; +}; + +export default provider; diff --git a/projects/app/src/web/support/user/api.ts b/projects/app/src/web/support/user/api.ts index ed68cd3a3..15bb53545 100644 --- a/projects/app/src/web/support/user/api.ts +++ b/projects/app/src/web/support/user/api.ts @@ -23,6 +23,7 @@ export const oauthLogin = (params: OauthLoginProps) => POST('/proApi/support/user/account/login/oauth', params); export const postFastLogin = (params: FastLoginProps) => POST('/proApi/support/user/account/login/fastLogin', params); +export const ssoLogin = (params: any) => GET('/proApi/support/user/account/sso', params); export const postRegister = ({ username,