From 2b010a5a44c1c25c105b1dbcc399cad3dc2dc392 Mon Sep 17 00:00:00 2001 From: qikongjian Date: Mon, 22 Sep 2025 13:22:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=B7=E6=AD=8C=E7=99=BB=E5=BD=95=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E7=94=A8code=E6=98=AFsso=5Fconfig=E8=B0=83=E7=94=A8fi?= =?UTF-8?q?nd=5Fby=5Fcode=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/server-setting/find_by_code/route.ts | 94 ++++++++++++++++++++ app/signup/page.tsx | 50 +++++++---- components/pages/login.tsx | 53 +++++++---- lib/server-config.ts | 88 ++++++++++++++++++ 4 files changed, 255 insertions(+), 30 deletions(-) create mode 100644 app/api/server-setting/find_by_code/route.ts create mode 100644 lib/server-config.ts diff --git a/app/api/server-setting/find_by_code/route.ts b/app/api/server-setting/find_by_code/route.ts new file mode 100644 index 0000000..dd178ae --- /dev/null +++ b/app/api/server-setting/find_by_code/route.ts @@ -0,0 +1,94 @@ +/** + * Server Setting API路由 + * 根据code获取服务端配置 + */ + +import { NextRequest, NextResponse } from 'next/server'; + +/** + * 根据code获取服务端配置 + * POST /api/server-setting/find_by_code + */ +export async function POST(request: NextRequest) { + try { + const body = await request.json(); + const { code } = body; + + console.log('📋 获取服务端配置请求:', { code }); + + if (!code) { + return NextResponse.json({ + code: 400, + successful: false, + message: 'code parameter is required', + data: null + }, { status: 400 }); + } + + // 根据code返回不同的配置,匹配实际API格式 + let responseData = null; + + switch (code) { + case 'sso_config': + // SSO配置 - 控制Google登录按钮显示 + responseData = { + id: 8, + code: 'sso_config', + value: '{\n "show": true\n}', + note: 'sso 入口', + updated_at: new Date().toISOString().slice(0, 19) + }; + break; + + case 'home_tabs': + // 首页标签配置示例 + responseData = { + id: 1, + code: 'home_tabs', + value: JSON.stringify([ + { + id: '1', + title: '推荐', + subtitle: '为您推荐', + status: 1, + sortOrder: 1, + videos: [] + } + ], null, 2), + note: '首页标签配置', + updated_at: new Date().toISOString().slice(0, 19) + }; + break; + + default: + // 默认返回空配置 + responseData = { + id: 0, + code: code, + value: '{}', + note: '默认配置', + updated_at: new Date().toISOString().slice(0, 19) + }; + break; + } + + const response = { + code: 0, + successful: true, + message: 'Success', + data: responseData + }; + + console.log('✅ 服务端配置获取成功:', { code, hasData: !!responseData }); + return NextResponse.json(response); + + } catch (error) { + console.error('❌ 获取服务端配置失败:', error); + return NextResponse.json({ + code: 500, + successful: false, + message: 'Internal server error', + data: null + }, { status: 500 }); + } +} diff --git a/app/signup/page.tsx b/app/signup/page.tsx index b71829f..5b21eb2 100644 --- a/app/signup/page.tsx +++ b/app/signup/page.tsx @@ -7,6 +7,7 @@ import { signInWithGoogle, sendVerificationLink, registerUserWithInvite } from " import { GradientText } from "@/components/ui/gradient-text"; import { GoogleLoginButton } from "@/components/ui/google-login-button"; import { Eye, EyeOff, Mail } from "lucide-react"; +import { isGoogleLoginEnabled } from "@/lib/server-config"; export default function SignupPage() { const [name, setName] = useState(""); @@ -26,9 +27,10 @@ export default function SignupPage() { const [googleLoading, setGoogleLoading] = useState(false); const [emailFocused, setEmailFocused] = useState(false); const [passwordFocused, setPasswordFocused] = useState(false); + const [showGoogleLogin, setShowGoogleLogin] = useState(false); const router = useRouter(); - // Handle scroll indicator for small screens + // Handle scroll indicator for small screens and load SSO config React.useEffect(() => { try { const url = new URL(window.location.href); @@ -40,6 +42,20 @@ export default function SignupPage() { sessionStorage.setItem("inviteCode", code); } } catch (err) {} + + // 检查是否启用Google登录 + const checkGoogleLoginStatus = async () => { + try { + const enabled = await isGoogleLoginEnabled(); + setShowGoogleLogin(enabled); + } catch (error) { + console.error("Failed to check Google login status:", error); + setShowGoogleLogin(false); + } + }; + + checkGoogleLoginStatus(); + const handleScroll = () => { const scrollableElement = document.querySelector('.signup-form > div:nth-child(2)'); const formElement = document.querySelector('.signup-form'); @@ -493,21 +509,25 @@ export default function SignupPage() { {/* Fixed Footer */}
- {/* 谷歌登录按钮已暂时注释 */} -
-
- or -
-
+ {/* Google登录按钮 - 根据服务端配置显示/隐藏 */} + {showGoogleLogin && ( + <> +
+
+ or +
+
- + + + )}

diff --git a/components/pages/login.tsx b/components/pages/login.tsx index 265a613..8defed2 100644 --- a/components/pages/login.tsx +++ b/components/pages/login.tsx @@ -9,6 +9,7 @@ import { signInWithGoogle, loginUser } from "@/lib/auth"; import { GradientText } from "@/components/ui/gradient-text"; import { GoogleLoginButton } from "@/components/ui/google-login-button"; import { Eye, EyeOff } from "lucide-react"; +import { isGoogleLoginEnabled } from "@/lib/server-config"; export default function Login() { const [email, setEmail] = useState(""); @@ -21,6 +22,7 @@ export default function Login() { const [googleLoading, setGoogleLoading] = useState(false); const [emailFocused, setEmailFocused] = useState(false); const [passwordFocused, setPasswordFocused] = useState(false); + const [showGoogleLogin, setShowGoogleLogin] = useState(false); const router = useRouter(); const searchParams = useSearchParams(); @@ -56,7 +58,7 @@ export default function Login() { } }; - // Check for registered=true parameter + // Check for registered=true parameter and load SSO config useEffect(() => { const registered = searchParams?.get("registered"); if (registered === "true") { @@ -77,6 +79,22 @@ export default function Login() { setFormError("The old OAuth method is deprecated. Please use the Google login button below for a better experience."); } } + + // 检查是否启用Google登录 + const checkGoogleLoginStatus = async () => { + try { + console.log('🔍 登录页面:开始检查Google登录状态...'); + const enabled = await isGoogleLoginEnabled(); + console.log('📋 登录页面:Google登录启用状态:', enabled); + setShowGoogleLogin(enabled); + console.log('📋 登录页面:设置showGoogleLogin状态为:', enabled); + } catch (error) { + console.error("❌ 登录页面:Failed to check Google login status:", error); + setShowGoogleLogin(false); + } + }; + + checkGoogleLoginStatus(); }, [searchParams]); const handleGoogleSignIn = async () => { @@ -262,21 +280,26 @@ export default function Login() { > {isSubmitting ? "Logging in..." : "Login"} - {/* 谷歌登录按钮已暂时注释 */} -

-
- or -
-
+ + {/* Google登录按钮 - 根据服务端配置显示/隐藏 */} + {showGoogleLogin && ( + <> +
+
+ or +
+
- + + + )}

diff --git a/lib/server-config.ts b/lib/server-config.ts new file mode 100644 index 0000000..c27e7e4 --- /dev/null +++ b/lib/server-config.ts @@ -0,0 +1,88 @@ +/** + * 服务端配置工具函数 + */ + +import { post } from '@/api/request'; + +/** + * SSO配置接口 + */ +export interface SSOConfig { + show: boolean; + provider: string; + displayName: string; + description: string; +} + +/** + * 获取SSO配置 + * @returns Promise + */ +export const getSSOConfig = async (): Promise => { + try { + console.log('🔍 开始获取SSO配置...'); + const res = await post(`/api/server-setting/find_by_code`, { code: 'sso_config' }); + + console.log('📋 SSO API响应:', res); + + if (!res || res.code !== 0 || !res.successful || !res.data) { + console.warn('❌ Failed to fetch SSO config:', res); + return null; + } + + // 新的数据格式:data直接包含id, code, value等字段 + const { value } = res.data; + console.log('📝 SSO配置原始value:', value); + console.log('📝 value类型:', typeof value, 'value长度:', value?.length); + + if (typeof value !== 'string' || value.length === 0) { + console.warn('❌ Invalid SSO config format:', value); + return null; + } + + try { + const config: SSOConfig = JSON.parse(value); + console.log('✅ SSO配置解析成功:', config); + return config; + } catch (parseError) { + console.error('❌ Failed to parse SSO config:', parseError); + console.error('❌ 原始value:', JSON.stringify(value)); + return null; + } + } catch (error) { + console.error('❌ Error fetching SSO config:', error); + return null; + } +}; + +/** + * 检查是否启用Google登录 + * @returns Promise + */ +export const isGoogleLoginEnabled = async (): Promise => { + try { + console.log('🔍 检查Google登录是否启用...'); + const config = await getSSOConfig(); + console.log('📋 获得的配置:', config); + + if (!config) { + console.log('❌ 没有获得配置,返回false'); + return false; + } + + const isEnabled = config?.show === true; + + console.log('🔍 配置检查:', { + show: config?.show, + provider: config?.provider, + isEnabled, + finalResult: isEnabled + }); + + // 简化逻辑:只检查show字段,因为sso_config专门用于Google登录 + return isEnabled; + } catch (error) { + console.error('❌ Error checking Google login status:', error); + return false; // 出错时默认不显示 + } +};