Skip to content

refactor: extract Google Analytics config to env and improve login redirect#141

Merged
Simpleyyt merged 4 commits intomainfrom
cursor/-bc-2c8e26a7-9c5a-44ff-b83a-9d13acd6ccfd-6723
Mar 29, 2026
Merged

refactor: extract Google Analytics config to env and improve login redirect#141
Simpleyyt merged 4 commits intomainfrom
cursor/-bc-2c8e26a7-9c5a-44ff-b83a-9d13acd6ccfd-6723

Conversation

@Simpleyyt
Copy link
Copy Markdown
Owner

@Simpleyyt Simpleyyt commented Mar 26, 2026

Summary

Google Analytics 配置外置

  • 将硬编码的 Google Analytics Measurement ID (G-XCRZ3HH31S) 从 frontend/src/main.ts 中移除
  • 在后端 Settings 中新增 google_analytics_id 配置项,通过 .env 环境变量设置
  • 通过 /api/v1/config/frontend 接口将 google_analytics_id 传递给前端
  • 前端启动时从后端配置动态初始化 vue-gtag,未配置时不加载 GA

登录页跳转优化

  • 在路由守卫中对 /login 路由也检查 auth_provider
  • auth_provider=none 时,访问 /login 会立即重定向到首页
  • authProvidernull(服务端不可用)时,与 none 同等处理——放行用户,不跳 /login

服务端不可用时防止路由跳转循环

  • getCachedClientConfig() 请求失败时标记 isClientConfigLoaded=true,缓存失败结果,避免每次导航都重试
  • authProvidernull 时守卫直接放行,不会把用户踢到同样不可用的 /login

Changed Files

File Change
backend/app/core/config.py 新增 google_analytics_id 设置
backend/app/interfaces/schemas/config.py ClientConfigResponse 新增 google_analytics_id 字段
backend/app/interfaces/api/config_routes.py 接口返回中传递 google_analytics_id
frontend/src/api/config.ts TS 接口新增 google_analytics_id;失败时缓存结果避免重复请求
frontend/src/main.ts 动态加载 GA + 优化登录页路由判断 + authProvidernull 时放行
.env.example 新增 GOOGLE_ANALYTICS_ID 模板
Open in Web Open in Cursor 

cursoragent and others added 4 commits March 26, 2026 16:35
…direct

- Add GOOGLE_ANALYTICS_ID to backend Settings and .env.example
- Pass google_analytics_id through /config/frontend API endpoint
- Frontend dynamically configures vue-gtag from backend config instead of hardcoded ID
- Improve login page redirect: also check auth_provider on /login route
  so that auth_provider=none users are redirected away from login page immediately

Co-authored-by: Simpleyyt <simpleyyt@gmail.com>
- Cache config load failure (set isClientConfigLoaded=true on error)
  so subsequent navigations don't retry the request every time
- Add configFailed early-return branch in route guard: when config
  is unavailable, fall back to pure token-based checks without
  authProvider-dependent redirects, avoiding redirect loops
- Export isConfigLoadFailed() and resetCachedClientConfig() for
  consumers that need to check or retry

Co-authored-by: Simpleyyt <simpleyyt@gmail.com>
The normal guard branches already degrade correctly when authProvider
is null (config unavailable): 'none' checks won't match, so it falls
through to pure token-based logic. No special configFailed branch needed.

Co-authored-by: Simpleyyt <simpleyyt@gmail.com>
When config fetch fails authProvider is null. Treat it the same as
'none' — let the user through instead of bouncing to /login where
the server is equally unreachable.

Co-authored-by: Simpleyyt <simpleyyt@gmail.com>
@Simpleyyt Simpleyyt marked this pull request as ready for review March 26, 2026 17:34
@Simpleyyt Simpleyyt merged commit d14112a into main Mar 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants