diff --git a/server/app.py b/server/app.py index f9711c5..14edb87 100644 --- a/server/app.py +++ b/server/app.py @@ -562,8 +562,13 @@ def recompute_trips(): @app.get("/api/trips") @require_login def portal_trips_compat(): - """Pi history.js compatibility alias.""" - return portal_trips() + """Pi history.js compatibility alias — wraps response in {trips: [...]}.""" + device_id = _portal_device_id() + if device_id is None: + return jsonify(error="device not configured"), 404 + limit = min(int(request.args.get("limit", 200)), 1000) + trips = db.list_trips(device_id, limit=limit) + return jsonify(trips=trips) @app.get("/api/trip/") diff --git a/server/modules/user_auth.py b/server/modules/user_auth.py index 7fb957d..5cb7c4c 100644 --- a/server/modules/user_auth.py +++ b/server/modules/user_auth.py @@ -38,10 +38,14 @@ def hash_password(plain): def require_login(view): - """Decorator: redirect to login page if not authenticated.""" + """Decorator: redirect to login page if not authenticated (HTML routes), + or return 401 JSON for /api/* routes.""" @wraps(view) def wrapped(*args, **kwargs): if not current_user.is_authenticated: + from flask import request, jsonify + if request.path.startswith("/api/"): + return jsonify(error="not authenticated"), 401 return redirect(url_for("login_page")) return view(*args, **kwargs) return wrapped diff --git a/static/js/history.js b/static/js/history.js index c9f9c1b..356b293 100644 --- a/static/js/history.js +++ b/static/js/history.js @@ -189,7 +189,7 @@ ${formatDistance(trip.distance_km)} ${formatDuration(trip.duration_seconds)} ${formatSpeed(trip.max_speed_kmh)} - ${trip.odo_start_km.toFixed(0)} → ${trip.odo_end_km.toFixed(0)} km + ${trip.odo_start_km != null ? trip.odo_start_km.toFixed(0) + ' → ' + trip.odo_end_km.toFixed(0) + ' km' : '—'}