Skip to content

Commit 838d0c4

Browse files
committed
fix: env var
1 parent ded01c5 commit 838d0c4

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

platforms/calendar/components/auth/login-screen.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import { useCallback, useEffect, useState } from 'react'
44
import { QRCodeSVG } from 'qrcode.react'
5-
import { calendarApi, parseSessionFromUri } from '@/lib/calendar-api'
5+
import {
6+
calendarApi,
7+
getCalendarApiUrl,
8+
parseSessionFromUri,
9+
} from '@/lib/calendar-api'
610
import { useAuth } from '@/contexts/auth-context'
711
import { Button } from '@/components/ui/button'
812
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
913

10-
const API_URL =
11-
process.env.NEXT_PUBLIC_CALENDAR_API_URL ?? "http://localhost:4001";
12-
1314
export default function LoginScreen() {
1415
const { login } = useAuth()
1516
const [uri, setUri] = useState<string>('')
@@ -22,6 +23,10 @@ export default function LoginScreen() {
2223
setLoading(true)
2324
setError(null)
2425
try {
26+
const apiUrl = getCalendarApiUrl()
27+
if (process.env.NODE_ENV === 'development') {
28+
console.log('[auth] fetching offer from', `${apiUrl}/api/auth/offer`)
29+
}
2530
const data = await calendarApi.getOffer()
2631
setUri(data.uri)
2732
const sid = data.sessionId ?? parseSessionFromUri(data.uri)
@@ -41,8 +46,9 @@ export default function LoginScreen() {
4146

4247
useEffect(() => {
4348
if (!sessionId) return
49+
const apiUrl = getCalendarApiUrl()
4450
const eventSource = new EventSource(
45-
`${API_URL}/api/auth/sessions/${sessionId}`
51+
`${apiUrl}/api/auth/sessions/${sessionId}`
4652
)
4753
eventSource.onmessage = (event) => {
4854
try {

platforms/calendar/lib/calendar-api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
// From root .env (loaded in next.config)
1+
// Single source: set in next.config from root .env and inlined into client bundle
22
const API_URL =
33
process.env.NEXT_PUBLIC_CALENDAR_API_URL ?? "http://localhost:4001";
4+
5+
export function getCalendarApiUrl(): string {
6+
return API_URL;
7+
}
8+
49
const TOKEN_KEY = "calendar-w3ds-token";
510

611
export function getToken(): string | null {

platforms/calendar/next.config.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import type { NextConfig } from "next";
22
import path from "path";
3-
import { loadEnvConfig } from "@next/env";
3+
import { readFileSync, existsSync } from "fs";
44

5-
// Load .env* from monorepo root so NEXT_PUBLIC_* and others come from root (Next.js load order)
6-
const rootDir = path.resolve(__dirname, "../..");
7-
loadEnvConfig(rootDir);
5+
// Read NEXT_PUBLIC_CALENDAR_API_URL from monorepo root .env so client bundle gets it (Next loads project .env first, so process.env can be wrong)
6+
function getRootEnv(name: string): string | undefined {
7+
const rootEnv = path.resolve(__dirname, "../../.env");
8+
if (!existsSync(rootEnv)) return undefined;
9+
const content = readFileSync(rootEnv, "utf-8");
10+
const match = new RegExp(`^${name}\\s*=\\s*(.+)$`, "m").exec(content);
11+
if (!match) return undefined;
12+
const raw = match[1].replace(/^["']|["']$/g, "").trim();
13+
const comment = raw.indexOf(" #");
14+
return comment >= 0 ? raw.slice(0, comment).trim() : raw;
15+
}
16+
17+
const calendarApiUrl =
18+
getRootEnv("NEXT_PUBLIC_CALENDAR_API_URL") ??
19+
process.env.NEXT_PUBLIC_CALENDAR_API_URL ??
20+
"http://localhost:4001";
821

922
const nextConfig: NextConfig = {
10-
/* config options here */
23+
env: {
24+
NEXT_PUBLIC_CALENDAR_API_URL: calendarApiUrl,
25+
},
1126
};
1227

1328
export default nextConfig;

0 commit comments

Comments
 (0)