From 52da1d8c11f357bf237f9a5c87ccac77f99db4da Mon Sep 17 00:00:00 2001 From: cy948 <67412196+cy948@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:48:49 +0800 Subject: [PATCH] :sparkles: feat: Skip login page if only one provider exists (#3400) --- src/layout/GlobalProvider/StoreInitialization.tsx | 2 ++ src/server/globalConfig/index.ts | 2 ++ src/store/serverConfig/selectors.ts | 1 + src/store/user/slices/auth/action.ts | 6 ++++++ src/store/user/slices/auth/initialState.ts | 3 ++- src/types/serverConfig.ts | 1 + 6 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/layout/GlobalProvider/StoreInitialization.tsx b/src/layout/GlobalProvider/StoreInitialization.tsx index a3e17d9ff86a..462b4b483a25 100644 --- a/src/layout/GlobalProvider/StoreInitialization.tsx +++ b/src/layout/GlobalProvider/StoreInitialization.tsx @@ -40,6 +40,8 @@ const StoreInitialization = memo(() => { const useUserStoreUpdater = createStoreUpdater(useUserStore); const enableNextAuth = useServerConfigStore(serverConfigSelectors.enabledOAuthSSO); useUserStoreUpdater('enabledNextAuth', enableNextAuth); + const oAuthSSOProviders = useServerConfigStore(serverConfigSelectors.oAuthSSOProviders); + useUserStoreUpdater('oAuthSSOProviders', oAuthSSOProviders); /** * The store function of `isLogin` will both consider the values of `enableAuth` and `isSignedIn`. diff --git a/src/server/globalConfig/index.ts b/src/server/globalConfig/index.ts index a12fa0d94f03..e001538cc229 100644 --- a/src/server/globalConfig/index.ts +++ b/src/server/globalConfig/index.ts @@ -1,4 +1,5 @@ import { appEnv, getAppConfig } from '@/config/app'; +import { authEnv } from '@/config/auth'; import { fileEnv } from '@/config/file'; import { langfuseEnv } from '@/config/langfuse'; import { getLLMConfig } from '@/config/llm'; @@ -126,6 +127,7 @@ export const getServerGlobalConfig = () => { zeroone: { enabled: ENABLED_ZEROONE }, zhipu: { enabled: ENABLED_ZHIPU }, }, + oAuthSSOProviders: authEnv.NEXT_AUTH_SSO_PROVIDERS.trim().split(/[,,]/), systemAgent: parseSystemAgent(appEnv.SYSTEM_AGENT), telemetry: { langfuse: langfuseEnv.ENABLE_LANGFUSE, diff --git a/src/store/serverConfig/selectors.ts b/src/store/serverConfig/selectors.ts index dbe0f2cca12f..253a16ae6c52 100644 --- a/src/store/serverConfig/selectors.ts +++ b/src/store/serverConfig/selectors.ts @@ -11,4 +11,5 @@ export const serverConfigSelectors = { enabledOAuthSSO: (s: ServerConfigStore) => s.serverConfig.enabledOAuthSSO, enabledTelemetryChat: (s: ServerConfigStore) => s.serverConfig.telemetry.langfuse || false, isMobile: (s: ServerConfigStore) => s.isMobile || false, + oAuthSSOProviders: (s: ServerConfigStore) => s.serverConfig.oAuthSSOProviders, }; diff --git a/src/store/user/slices/auth/action.ts b/src/store/user/slices/auth/action.ts index f501793b5fc9..f5b6fac1b2ee 100644 --- a/src/store/user/slices/auth/action.ts +++ b/src/store/user/slices/auth/action.ts @@ -49,6 +49,12 @@ export const createAuthSlice: StateCreator< const enableNextAuth = get().enabledNextAuth; if (enableNextAuth) { const { signIn } = await import('next-auth/react'); + // Check if only one provider is available + const providers = get()?.oAuthSSOProviders; + if (providers && providers.length === 1) { + signIn(providers[0]); + return; + } signIn(); } }, diff --git a/src/store/user/slices/auth/initialState.ts b/src/store/user/slices/auth/initialState.ts index 6d35b3a3b36e..8fb0b4cd4afe 100644 --- a/src/store/user/slices/auth/initialState.ts +++ b/src/store/user/slices/auth/initialState.ts @@ -17,11 +17,12 @@ export interface UserAuthState { clerkSignOut?: SignOut; clerkUser?: UserResource; enabledNextAuth?: boolean; - isLoaded?: boolean; + isSignedIn?: boolean; nextSession?: Session; nextUser?: User; + oAuthSSOProviders?: string[]; user?: LobeUser; } diff --git a/src/types/serverConfig.ts b/src/types/serverConfig.ts index c2d169f27744..99a30df31650 100644 --- a/src/types/serverConfig.ts +++ b/src/types/serverConfig.ts @@ -25,6 +25,7 @@ export interface GlobalServerConfig { enabledAccessCode?: boolean; enabledOAuthSSO?: boolean; languageModel?: ServerLanguageModel; + oAuthSSOProviders?: string[]; systemAgent?: DeepPartial; telemetry: { langfuse?: boolean;