Skip to content

Commit 3b6634e

Browse files
committed
refactor: add api key and docker url
1 parent 3334ac1 commit 3b6634e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

frontend/.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ NEXT_PUBLIC_API_URL=http://localhost:3000/api/v1
22
NEXT_PUBLIC_STORAGE_URL=http://localhost:3000/api/assets
33
NEXT_PUBLIC_MIXPANEL_TOKEN=f2e8a71ab2bde33ebf346c5abf6ba9fa
44
NEXT_PUBLIC_ROLLBAR_ACCESS_TOKEN=0df0bee895044430880278e2b2a5b2d2
5+
# NEXT_PUBLIC_API_KEY=xxx-xxx-xxx-xxx # Uncomment this if you have an API key
6+
# NEXT_PUBLIC_BACKEND_URL=http://backend:5328 # Uncomment this if you're working with a docker setup

frontend/src/middleware.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@ import type { NextRequest } from "next/server";
33
import localStorage from "@/lib/localStorage";
44
import { APIKeyData } from "./interfaces/user";
55
import axios from "axios";
6+
import { GetAPIKey } from "./services/user";
67

78
export async function middleware(request: NextRequest) {
89
let apiKey = null;
910
try {
10-
// Hardcoded for now for the docker
11-
const response = await axios.get<{ data: APIKeyData }>(
12-
`http://backend:5328/v1/user/get-api-key`
13-
);
11+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
12+
const dockerBackendUrl = process.env.NEXT_PUBLIC_BACKEND_URL;
1413

15-
apiKey = { data: { api_key: response.data.data.key } };
14+
if (envApiKey) {
15+
apiKey = { data: { api_key: envApiKey } };
16+
} else if (dockerBackendUrl) {
17+
const response = await axios.get<{ data: APIKeyData }>(
18+
`${dockerBackendUrl}/v1/user/get-api-key`
19+
);
20+
apiKey = { data: { api_key: response.data.data.key } };
21+
} else {
22+
apiKey = await GetAPIKey();
23+
}
1624
} catch (error) {
1725
console.error("Error fetching API key:", error);
1826
return NextResponse.redirect(new URL("/api-key-setup", request.url));

0 commit comments

Comments
 (0)