From 5c4c0ae49ddd5d76486359add7b10ec214683e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ullrich=20Sch=C3=A4fer?= Date: Wed, 15 Apr 2026 07:12:13 +0200 Subject: [PATCH] Fix route compute 401: use authenticated API client The route editor called fetch() directly for /api/v1/routes/compute without the bearer token. Now uses the API client which injects auth headers and handles 401 auto-refresh. Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/mobile/lib/api-client.ts | 7 +++++++ apps/mobile/lib/editor/use-route-editor.ts | 23 +++++----------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/apps/mobile/lib/api-client.ts b/apps/mobile/lib/api-client.ts index f9d1732..f5da29a 100644 --- a/apps/mobile/lib/api-client.ts +++ b/apps/mobile/lib/api-client.ts @@ -113,6 +113,13 @@ export function updateRoute(id: string, data: { name?: string; description?: str }); } +export function computeRoute(waypoints: Array<{ lat: number; lon: number }>, profile = "fastbike") { + return request("/routes/compute", { + method: "POST", + body: JSON.stringify({ waypoints, profile }), + }); +} + // --- Activities --- export async function listActivities(cursor?: string, limit = 20) { diff --git a/apps/mobile/lib/editor/use-route-editor.ts b/apps/mobile/lib/editor/use-route-editor.ts index 08dad73..f6b19a4 100644 --- a/apps/mobile/lib/editor/use-route-editor.ts +++ b/apps/mobile/lib/editor/use-route-editor.ts @@ -1,8 +1,7 @@ import { useState, useCallback, useRef } from "react"; import type { Waypoint } from "@trails-cool/types"; import type { RouteDetail } from "../api-client"; -import { updateRoute } from "../api-client"; -import { getServerUrl } from "../server-config"; +import { updateRoute, computeRoute as apiComputeRoute } from "../api-client"; import { generateGpx } from "@trails-cool/gpx"; export interface RouteSegment { @@ -44,22 +43,10 @@ export function useRouteEditor(route: RouteDetail) { setState((s) => ({ ...s, computing: true, error: null })); try { - const serverUrl = await getServerUrl(); - const resp = await fetch(`${serverUrl}/api/v1/routes/compute`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - waypoints: waypoints.map((w) => ({ lat: w.lat, lon: w.lon })), - profile: route.routingProfile ?? "fastbike", - }), - }); - - if (!resp.ok) { - setState((s) => ({ ...s, computing: false, error: "Route computation failed" })); - return; - } - - const geojson = await resp.json(); + const geojson = await apiComputeRoute( + waypoints.map((w) => ({ lat: w.lat, lon: w.lon })), + route.routingProfile ?? "fastbike", + ); const coords = extractCoordsFromGeojson(geojson); setState((s) => ({ ...s,