Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions app/api/exchange-rates/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ export async function GET(req: NextRequest) {

try {
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "https://nexafx-backend.onrender.com/v1";

// Forward the caller's token when present. The backend may treat /exchange-rates
// as public or optionally authenticated (e.g. for personalised rates); unauthenticated
// requests still proceed without an Authorization header.
const token =
req.headers.get("x-client-token") ??
req.cookies.get("access_token")?.value;

const headers = new Headers({ "Content-Type": "application/json" });
if (token) {
headers.set("Authorization", `Bearer ${token}`);
}

const externalRes = await fetch(`${BASE_URL}/exchange-rates?from=${from}&to=${to}`, {
headers: {
"Content-Type": "application/json",
},
headers,
});

if (!externalRes.ok) {
Expand All @@ -30,7 +41,7 @@ export async function GET(req: NextRequest) {

const data = await externalRes.json();
return NextResponse.json(data);
} catch (error: unknown) {
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch exchange rates' },
{ status: 500 }
Expand Down
10 changes: 9 additions & 1 deletion lib/api/exchange-rates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export async function getExchangeRate(from: string, to: string) {
const token =
typeof window !== "undefined" ? localStorage.getItem("access_token") : null;

const headers = new Headers({ "Content-Type": "application/json" });
if (token) {
headers.set("x-client-token", token);
}

const res = await fetch(`/api/exchange-rates?from=${from}&to=${to}`, {
method: "GET",
headers: { "Content-Type": "application/json" },
headers,
});

if (!res.ok) {
Expand Down
Loading