Skip to content

Commit

Permalink
✨ feat: Skip login page if only one provider exists (lobehub#3400)
Browse files Browse the repository at this point in the history
  • Loading branch information
cy948 authored Aug 5, 2024
1 parent 46e7916 commit 52da1d8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/layout/GlobalProvider/StoreInitialization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 2 additions & 0 deletions src/server/globalConfig/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/store/serverConfig/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
6 changes: 6 additions & 0 deletions src/store/user/slices/auth/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/store/user/slices/auth/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/serverConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface GlobalServerConfig {
enabledAccessCode?: boolean;
enabledOAuthSSO?: boolean;
languageModel?: ServerLanguageModel;
oAuthSSOProviders?: string[];
systemAgent?: DeepPartial<UserSystemAgentConfig>;
telemetry: {
langfuse?: boolean;
Expand Down

0 comments on commit 52da1d8

Please sign in to comment.