outshine84 commited on
Commit
b6d2d7f
·
1 Parent(s): e3090a7

Load Google client ID from runtime config

Browse files
Files changed (3) hide show
  1. api.py +12 -0
  2. pwa-app/src/App.jsx +2 -3
  3. pwa-app/src/main.jsx +44 -12
api.py CHANGED
@@ -455,6 +455,13 @@ def get_search_backend_status():
455
  return {"ready": ready, "modules": checks, "files": files}
456
 
457
 
 
 
 
 
 
 
 
458
  class PlantChatRequest(BaseModel):
459
  plant_name: str = Field(..., min_length=2, description="Nome comune o scientifico della pianta")
460
  question: str = Field(..., min_length=3, description="Domanda sulla cura della pianta")
@@ -794,6 +801,11 @@ def search_status():
794
  return get_search_backend_status()
795
 
796
 
 
 
 
 
 
797
  @app.get("/species/previews")
798
  def species_previews(
799
  names: list[str] = Query(default=[], description="Nomi specie da risolvere per anteprima immagine"),
 
455
  return {"ready": ready, "modules": checks, "files": files}
456
 
457
 
458
+ def get_public_app_config() -> dict[str, Any]:
459
+ return {
460
+ "google_client_id": GOOGLE_CLIENT_IDS[0] if GOOGLE_CLIENT_IDS else "",
461
+ "require_google_auth": REQUIRE_GOOGLE_AUTH,
462
+ }
463
+
464
+
465
  class PlantChatRequest(BaseModel):
466
  plant_name: str = Field(..., min_length=2, description="Nome comune o scientifico della pianta")
467
  question: str = Field(..., min_length=3, description="Domanda sulla cura della pianta")
 
801
  return get_search_backend_status()
802
 
803
 
804
+ @app.get("/app-config")
805
+ def app_config():
806
+ return JSONResponse(content=get_public_app_config())
807
+
808
+
809
  @app.get("/species/previews")
810
  def species_previews(
811
  names: list[str] = Query(default=[], description="Nomi specie da risolvere per anteprima immagine"),
pwa-app/src/App.jsx CHANGED
@@ -109,7 +109,7 @@ function formatISOToInputDate(value) {
109
  return `${year}-${month}-${day}`;
110
  }
111
 
112
- export default function App() {
113
  const [auth, setAuth] = useState(null);
114
  const [authBusy, setAuthBusy] = useState(false);
115
  const [file, setFile] = useState(null);
@@ -160,7 +160,6 @@ export default function App() {
160
 
161
  const canAsk = selectedSpecies && question.trim().length > 2;
162
  const isLoggedIn = Boolean(auth?.idToken);
163
- const googleClientIdConfigured = Boolean(import.meta.env.VITE_GOOGLE_CLIENT_ID);
164
  const galleryImages = useMemo(() => {
165
  if (!plantCard?.images?.length) {
166
  return [];
@@ -756,7 +755,7 @@ export default function App() {
756
  <div className="auth-box">
757
  {!googleClientIdConfigured && (
758
  <p className="auth-warning">
759
- Imposta VITE_GOOGLE_CLIENT_ID per abilitare il login con Google.
760
  </p>
761
  )}
762
 
 
109
  return `${year}-${month}-${day}`;
110
  }
111
 
112
+ export default function App({ googleClientIdConfigured = false }) {
113
  const [auth, setAuth] = useState(null);
114
  const [authBusy, setAuthBusy] = useState(false);
115
  const [file, setFile] = useState(null);
 
160
 
161
  const canAsk = selectedSpecies && question.trim().length > 2;
162
  const isLoggedIn = Boolean(auth?.idToken);
 
163
  const galleryImages = useMemo(() => {
164
  if (!plantCard?.images?.length) {
165
  return [];
 
755
  <div className="auth-box">
756
  {!googleClientIdConfigured && (
757
  <p className="auth-warning">
758
+ Configura GOOGLE_CLIENT_ID nello Space per abilitare il login con Google.
759
  </p>
760
  )}
761
 
pwa-app/src/main.jsx CHANGED
@@ -5,7 +5,35 @@ import { registerSW } from "virtual:pwa-register";
5
  import App from "./App";
6
  import "./styles.css";
7
 
8
- const googleClientId = import.meta.env.VITE_GOOGLE_CLIENT_ID || "";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  registerSW({
11
  immediate: true,
@@ -16,14 +44,18 @@ registerSW({
16
  }
17
  });
18
 
19
- ReactDOM.createRoot(document.getElementById("root")).render(
20
- <React.StrictMode>
21
- {googleClientId ? (
22
- <GoogleOAuthProvider clientId={googleClientId}>
23
- <App />
24
- </GoogleOAuthProvider>
25
- ) : (
26
- <App />
27
- )}
28
- </React.StrictMode>
29
- );
 
 
 
 
 
5
  import App from "./App";
6
  import "./styles.css";
7
 
8
+ function getApiBase() {
9
+ if (import.meta.env.VITE_API_BASE) {
10
+ return import.meta.env.VITE_API_BASE;
11
+ }
12
+ if (window.location.port === "5173") {
13
+ return "http://localhost:8000";
14
+ }
15
+ return "";
16
+ }
17
+
18
+ async function loadAppConfig() {
19
+ const buildTimeGoogleClientId = import.meta.env.VITE_GOOGLE_CLIENT_ID || "";
20
+ if (buildTimeGoogleClientId) {
21
+ return { googleClientId: buildTimeGoogleClientId };
22
+ }
23
+
24
+ try {
25
+ const apiBase = getApiBase();
26
+ const response = await fetch(`${apiBase}/app-config`);
27
+ if (!response.ok) {
28
+ throw new Error(`HTTP ${response.status}`);
29
+ }
30
+ const data = await response.json();
31
+ return { googleClientId: data.google_client_id || "" };
32
+ } catch (error) {
33
+ console.warn("Impossibile caricare la configurazione runtime dell'app", error);
34
+ return { googleClientId: "" };
35
+ }
36
+ }
37
 
38
  registerSW({
39
  immediate: true,
 
44
  }
45
  });
46
 
47
+ const root = ReactDOM.createRoot(document.getElementById("root"));
48
+
49
+ loadAppConfig().then(({ googleClientId }) => {
50
+ root.render(
51
+ <React.StrictMode>
52
+ {googleClientId ? (
53
+ <GoogleOAuthProvider clientId={googleClientId}>
54
+ <App googleClientIdConfigured />
55
+ </GoogleOAuthProvider>
56
+ ) : (
57
+ <App googleClientIdConfigured={false} />
58
+ )}
59
+ </React.StrictMode>
60
+ );
61
+ });